Weird! A cookie can't be set.

Setting a cookie is a straight forward task. I did it more than once. This time, however, I can't set a cookie up. The cookies is created properly. And it is added into the HttpservletResponse. I think there are two possibilities. One is that the cookie is not added into the response properly. I dont' know how to find out whether a response carry a cookie or not. The other is the client doesn't take it. Since my Firefox browser takes the session cookie from the web application, I believe the browser accept a cookie from the Servlet/JSP application.

Any suggestion on how to resolve this problem?

ps. I use TC 5.5.20.

Thanks in advance.

[670 byte] By [vwuvancouvera] at [2007-11-26 22:48:45]
# 1
You might want to post your codes that set the cookie.
singchyuna at 2007-7-10 12:08:46 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Here is the code for setting a cookie in the standard way.

Cookie c = new Cookie("my_cookie", (new java.util.Date())

.toString());

c.setMaxAge(3600);

response.addCookie(c);

And here is the code for retrieving a cookie also in the stand way.

Cookie[] cookies = request.getCookies();

if (cookies != null) {

for (int i = 0; i < cookies.length; i++) {

logger.debug("Cookie: " + cookies[i].getValue());

if (cookies[i].getName().equals("my_cookie")) {

logger.debug("Found the cookie: " + cookies[i].getValue());

break;

}

}

}

I have tested this issue with the following approach. After setting a cookie, forward to another method to check whether it is set or not. And, it is there. So, I believe there is a problem on the client, my browser in this case, doesn't take the cookie for some reason. Although, it takes cookies from the container (session ID) and from the web framework.

vwuvancouvera at 2007-7-10 12:08:46 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
> I dont' know how to find out whether a response carry a cookie or not.You are using Firefox, so you could install its plugin for Live HTTP Headers which will tell you that sort of thing.
DrClapa at 2007-7-10 12:08:46 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Are you sure you're setting the cookie in the right response? Maybe you've built an wrapped response or so.
BalusCa at 2007-7-10 12:08:46 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
Thanks for the tip. Now, I can inspect the cookies easily with the Live HTTP Header.
vwuvancouvera at 2007-7-10 12:08:46 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

After had that a cookie was set successfully in another method of the application, I suspect that was something wrong on the response, might due to action flow, such as forwarding afterward of an action. The response is a clear one, HttpServletResponse, not a wrapping class on it.

Thanks for your thought.

vwuvancouvera at 2007-7-10 12:08:46 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...