Write/Set Cookies
Date now = new Date();
long nowLong = now.getTime();
nowLong = nowLong + (1000L * 60 * 60 * 24 * 7);//seven days
now.setTime(nowLong);
Cookies.setCookie("sampleCookieName", "sampleCookiValue", now);
When retrieving the cookies, you have to specify only the name of the cookie, nothing related to duration. If the cookie is found in the browser for this domain (not expired); value you set will be returned.
Read/Get Cookies
Cookies.getCookie("sampleCookieName");//only name
Things to consider
In setting cookies, you must consider on what you actually plans to get done using a cookie.There are two possibilities in setting duration for cookies. Either remember duration
(i) from the day it's created.
(ii) from the last day this particular user viewed your site.
If your site always set cookies when ever a user visits your site; then your cookies will expire only after the user does not revisit your site for the specified duration. But if you are providing a feature like "Saving the password for 2 weeks", then you probably should store the cookie only if the cookie does not exists. For that you must look for cookie before setting it again.
String sampleValue = Cookies.getCookie("sampleCookieName");
if(sampleValue == null){
// cookie not found
// set cookie again after informing user on expiration.
}
0 Response to "Set Cookies with GWT applications to expire after expected time period"
Post a Comment