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.
# 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.
# 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.