2
votes

Environment

  • Eclipse Juno Service Release 1
  • GWT 2.5
  • Google Chrome with the GWT developer plugin
  • Running GWT on the Jetty server using 'run application' as Google web application

I am trying to set 2 cookies using gwt using the following code:

if(result.getStatus() == ServerResponse.SUCCESS) {
    System.out.println("I will now set cookies: " + result.getMessage() + " and " + Integer.toString(result.getValue().getId()));
    Cookies.setCookie("opsession", result.getMessage(), new Date(System.currentTimeMillis() + ClientUser.SESSION_EXPIRY_TIME_IN_MINUTES));
    Cookies.setCookie("opuser", Integer.toString(result.getValue().getId()), new Date(System.currentTimeMillis() + ClientUser.SESSION_EXPIRY_TIME_IN_MINUTES));
    System.out.println("Cookie set: session: " + Cookies.getCookie("opsession"));
    main.checkLoginAndRedirect();
    System.out.println("Redirected.");
} else if(result.getStatus() == ServerResponse.FAILURE){
    new MessageBox(result.getShortTitle(), result.getMessage()).show();
}

It doesn't seem to work. The println's are there for debugging, and here is the output:

I will now set cookies: 1er1qmaly9ker and 1
Cookie set: session: null
nullnull
Redirected.

ClientUser.SESSION_EXPIRY_TIME_IN_MINUTES is (was an int) a long that returns 20.

Update

Using

if(Cookies.getCookie("opsession") == null) {
    System.out.println("opsession: cookie not found at all.");
}

I've confirmed that the cookie is not placed at all, and does not have a 'null' String value.

I've also changed ClientUser.SESSION_EXPIRY_TIME_IN_MINUTES into a long.

Update

Fiddler confirms that cookie data has been sent:

Response sent 30 bytes of Cookie data: Set-Cookie: JSESSIONID=4kr11hs48gvq;Path=/

But only if I use the longer version of setCookie:

Cookies.setCookie("opuser", Integer.toString(result.getValue().getId()), new Date(System.currentTimeMillis() + ClientUser.SESSION_EXPIRY_TIME_IN_MINUTES), null, "/", false);

If I use the String, String, long variant, fiddler notices no cookie data.

1
Shouldn't that be ClientUser.SESSION_EXPIRY_TIME_IN_MINUTES * 60000 to convert it to milliseconds. - Nick Wilson
You have my incredibly sincere thanks! I used it this way because on the server, I use the line HttpSession.setMaxInactiveInterval() which takes time in seconds! Please post this as an answer, because I very much want to check it off! :) - Mark Tielemans

1 Answers

2
votes

Should be

new Date(System.currentTimeMillis()
+ (ClientUser.SESSION_EXPIRY_TIME_IN_MINUTES * 60000)))

to convert the expiry time to milliseconds.