Java News Tips Software
Java News Tips Software | Contact | Facebook | Twitter RSS

Set Cookies with GWT applications to expire after expected time period

Google Web Toolkit (GWT) supports HTTP cookies similar to other web technologies. GWT provides methods for setting cookies for specified time duration, for specific domains and paths. Below is a listing on how to set a basic cookie for a duration of one day.
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);

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.
Cookies.getCookie("sampleCookieName");//only name

In setting cookies, you must consider on what you actually plans to get done using a cookie. There are two features you can achieve; 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){
//set cookie again after informing user on expiration.}

Labels: , , , ,


7 Comments

  1. Anonymous Anonymous on January 07, 2009 11:45 AM  
    Hi,

    Could you please provide a sample program with setting and retrieving cookies,. it would be a great helpful for beginners like me in GWT.

    Regards,
    Venkat
  2. Anonymous Anonymous on February 20, 2009 2:28 PM  
    I agree with Venkat

    Numero 5
  3. There is a bug in your code. Try the following:
    nowLong = nowLong + (1000 * 60 * 60 * 24 * 365);

    that will result in setting the cookie in the past. You need to specify that you are using a long:
    nowLong = nowLong + (1000l * 60l * 60l * 24l * 365l);

    /José
  4. /**
    * Saves the cookie with name "cookieTest".
    *
    */
    protected void saveTestCookie(final String value) {
    final Date expireDtm = new Date(System.currentTimeMillis()
    + (1000 * 60 * 60 * 24 * 7)); /* seven days */
    Cookies.setCookie("cookieTest", value, expireDtm, null,
    null, true);
    Cookies.setCookie("passwordValue", passwordValue, expireDtm, "www.mysite.com", null, true);
    }

    /**
    * Gets the cookie value.
    */
    private String getTestCookie() {
    return Cookies.getCookie("cookieTest");
    }
  5. Anonymous Anonymous on October 04, 2009 11:21 AM  
    Thanks! It Helps!
  6. Anonymous Anonymous on November 11, 2009 5:10 PM  
    This comment has been removed by a blog administrator.
  7. Anonymous Anonymous on June 10, 2010 3:27 PM  
    Two things I would add:
    1) it is dangerous not to user longs when adding milliseconds (if you are above 2^16 you will have an overflow resulting in a negative value, eg one month = 3600*1000*24*30>2^16-->you will add a negative value and therefore go in the past)
    2) there is a simpler and cleaner way to get a future date:
    new Date(System.currentTimeMillis()+(1000L*3600L*24L*30L))
ABOUT AUTHOR
Page Views :
Email :
PREVIOUS ARTICLES
Select Month:
TOP
Free counter and web stats