开发者

GWT Cookies.getCookie returns "null"

Update: I tried clearing the created cook开发者_运维百科ie 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?


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


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.


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.


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜