tags:

views:

835

answers:

4

Update: I tried clearing the created cookie in the browser and trying it again, and it didn't happen. Conceivably I set a cookie with the value "null" at some point.

(Ok, this is probably a retorical question, so I'm making it CW)

The documentation for Google Web Toolkit says this about Cookies.getCookie:

public static java.lang.String getCookie(java.lang.String name)

Gets the cookie associated with the given name.

Parameters:

  • name - the name of the cookie to be retrieved

Returns:

  • the cookie's value, or null if the cookie doesn't exist

Well, I've just spent a number of hours beating my head against a wall because at least in the hosted mode browser (I haven't tested with a real browser yet), it doesn't return null, it returns "null", ie the literal string, 4 characters long starting with "n".

Both null and "null" look remarkably similar if you print them out, but only one responds to a if (cookie == null) Cookies.setCookie(cookie, newValue);

Is there any conceivable reason why Google did it this way, or is somebody just screwing me around?

A: 

Maybe trying with a duration can change the situation. Try this:

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);
dkberktas
My point was that I didn't set a cookie, and getCookie was returning the string "null" instead of a real null value like the documentation implied strongly.
Paul Tomblin
+1  A: 

are you sure there isn't a cookie set to a value of "null"? You should have a look at the headers on the response, just to make sure. Depending on the version of GWT this is possible in different ways -- easiest might be hitting "Compile" and trying a real browser, they make it easy to see the headers.

This happened before I had the setCookie code in there - I just wondered why my "cookie generation" code wasn't getting executed. Unfortunately it only seems to happen on the Hosted Mode browser, and I don't know how to clear cookies on that browser to test the problem.
Paul Tomblin
you can delete a cookie from the client side by making it expired. http://blogs.x2line.com/al/articles/316.aspx -- you can also list the cookies client side to see what's there.
A: 

Hi,

I can understand your headache (I posted a bug about gwt cookie documentation a while ago: http://code.google.com/p/google-web-toolkit/issues/detail?id=387&can=1 )

Which version of GWT are you using?

Which browser did you test in?

I just looked at the code for 1.6.4 (they ship the source), and I'd encourage you to file this as a bug. See issue 2994 for something close, but I think this is different enough to warrent its own bug filing.

It looks like GWT handles hashmaps in a different manner (for performance reasons?) than regular hashmaps; see java.util.AbstractHashMap in the com/google/gwt/emul directory when you unpack the gwt-user.jar file. Here's the get() impelementation.

   return (key == null) ? nullSlot : (!(key instanceof String) ? getHashValue(
   key, getHashCode(key)) : getStringValue((String) key));

And maybe this is the issue.

Hope this helps.

Dan

mooreds
A: 

I think setting a cookie null makes the value of the cookie "null" (String) You should remove the cookie with Cookies.removeCookie("CookieName") which should delete the cookie and your query will return the real null not the string one.

youmute