GWT Cookies.getCookie returns "null"

Update . I tried to clear the created cookie in the browser and try again, and this did not happen. It is clear that I set a cookie with the value "null" at some point.

(Well, this is probably a rhetorical question, so I do this CW)

The documentation for the Google Web Toolkit says Cookies.getCookie:

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

Gets the cookie associated with this name.

Parameters:

  • name - name of the cookie received

Return:

  • cookie or null if cookie does not exist

Well, I just spent a few hours hitting my head against the wall, because at least in a browser with hosted mode (I have not tested a real browser yet), it does not return null, it returns "null" , ie . a literal string, 4 characters long, starting with "n".

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

Is there any conceivable reason why Google did it this way, or is someone just fooling me?

+4
source share
4 answers

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

What version of GWT are you using?

Which browser did you check in?

I just looked at the code for 1.6.4 (they send the source), and I would recommend that you write this as an error. See Question 2994 for something close, but I think it is completely different to prevent his own error reporting.

GWT seems to handle hashmaps in a different way (for performance reasons?) Than regular hashmaps; see java.util.AbstractHashMap in the com / google / gwt / emul directory when unpacking the gwt-user.jar file. Here get () impelementation.

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

And perhaps this is a problem.

Hope this helps.

Dan

+1
source

Are you sure there is no cookie value for "null"? You should take a look at the response headers just to make sure. Depending on the version of GWT, this is possible in different ways - the simplest way is to hit "Compile" and try a real browser, they make it easier to view the headers.

+1
source

I think setting a null cookie makes the cookie "null" (String) You must remove the cookie from Cookies.removeCookie ("CookieName"), which should delete the cookie, and your request will return a real null, not a string.

+1
source

A lengthy attempt may make a difference. Try the following:

 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
source

All Articles