Benutzer-Werkzeuge

Webseiten-Werkzeuge


Sidebar

Navigation

Tags

gwt:cookie

GWT and Cookies

Setting cookies is easy like that, take the example project replace the onModuleLoad() function with the following, compile and run the following example.

public void onModuleLoad() {
	final TextBox nameField = new TextBox();
 
	//get the cookie with name MyCookie
	String msg = Cookies.getCookie("MyCookie");
 
	//if null, there was no cookie
	if(msg == null) msg = "0"; 
 
	//set a new value
	msg = ""+(Integer.parseInt(msg)+1);		
	Cookies.setCookie("MyCookie",msg);
 
	//dipslay the cookie
	nameField.setText(msg);
	RootPanel.get("nameFieldContainer").add(nameField);
	}

Cookies for a specified time

Date now = new Date();
long nowLong = now.getTime();
nowLong = nowLong + (1000 * 60 * 60 * 24 * 7);//seven days
now.setTime(nowLong);
Cookies.setCookie("sampleCookieName", "sampleCookiValue", now);
  • getCookie(String)
    • Gets the cookie associated with the given name.
  • getCookieNames()
    • Gets the names of all cookies in this page's domain.
  • removeCookie(String)
    • Removes the cookie associated with the given name.
  • setCookie(String, String)
    • Sets a cookie with name and value.
  • setCookie(String, String, Date)
    • Sets a cookie with name, value and expiring date.
  • setCookie(String, String, Date, String, String, boolean)
    • Sets a cookie with name, value, expiring date, domain of the cookie, the path associated with the cookie and true to make that cookie secure.
gwt/cookie.txt · Zuletzt geändert: 2012/09/30 22:04 von ben