Question

I had a GWT servelt on whose doGet method, I created a cookie as shown below:

Cookie nameCookie = new Cookie("name","adam");
response.addCookie(nameCookie);

I tried to read this on the client side as

String name = Cookies.getCookie("name");

But the output of string variable name was coming out as undefined.

Was it helpful?

Solution

I solved it by finding out that while creating a cookie, you also have to set the path for it. So in the server side,

Cookie nameCookie = new Cookie("name","adam");
nameCookie.setPath("/");
response.addCookie(nameCookie);

Now the following client side code returns the proper value as adam

String name = Cookies.getCookie("name");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top