Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting persistent cookie from Java doesn't work in IE

All,

Although I see related topics on the forum, but I don't see a clear solution on this issue. I am trying to set a javax.servlet.http.Cookie with an expiration time (so that it persists across browser sessions). Code:

public void respond(HttpServletRequest req, HttpServletResponse resp) {
    int expiration = 3600;
    Cookie cookie = new Cookie("TestCookie", "xyz");
    cookie.setDomain("");
    cookie.setVersion(0);
    cookie.setPath("/");
    cookie.setMaxAge(expiration);
    cookie.setSecure(false);
    resp.addCookie(cookie);
}

I don't see this cookie being set when I check in IE developer tools. Searching on the internet gave me clues that IE doesn't consider Max-Age, but only works with Expires. If this does not work for IE, then is there a proven way of setting the HTTP response headers for a persistent cookie so that it works for IE?

PS: This works fine on all other browsers.

I tried creating a string for the cookie having expires attribute. IE succeeded in creating it, but it lost the domain (default - "") and showed ".com" and turned it into a session cookie instead of a persistent cookie. This again works fine on all other browsers.

Please help. Thanks.

like image 966
thebigg Avatar asked Nov 11 '09 16:11

thebigg


1 Answers

Working with IE9, I found that it was the HttpOnly attribute that was required in order to get it to echo the cookie value on subsequent posts, e.g:

Set-Cookie: autologCk1=ABCD; Path=/autolog/; HttpOnly
like image 129
eric gilbertson Avatar answered Oct 20 '22 06:10

eric gilbertson