Setting persistent cookies with Java does not work in IE

Everything,

Although I see related topics on the forum, I do not see a clear solution to this problem. I am trying to set javax.servlet.http.Cookie with an expiration date (so that it is saved in browser sessions). The 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 do not see this cookie set when I check the IE developer tools. A search on the Internet made it clear that IE does not take into account Max-Age, but only works with Expires. If this does not work for IE, is there a proven way to set the HTTP response headers for a persistent cookie so that it works for IE?

PS: This works great in all other browsers.

I tried to create a string for the cookie with the expires attribute. IE managed to create it, but it lost the domain (the default is β€œ)” and showed β€œ.com” and turned it into a session cookie instead of a persistent cookie. This works fine again in all other browsers.

Please, help. Thanks.

+7
cookies persistent
source share
3 answers

When working with IE9, I found that it was an HttpOnly attribute, which was required in order to make it repeat the cookie value for subsequent messages, for example:

 Set-Cookie: autologCk1=ABCD; Path=/autolog/; HttpOnly 
+4
source share

Response to Persistent cookies from a servlet in IE .

In your case, there may be a different taste of the same problem: that is, the domain prefix with "." (I'm sure this is a cookie function of version 1), something on the Java stack decides that it is a cookie of version 1 (unrecognized and not saved by IE, even IE8) and sends this cookie format.

Or, as this answer suggests, is there something in your cookie value that contains an unrecognized character?

+1
source share

Since javax.servlet.http.Cookie does not allow you to set the Expires attribute to a cookie, you must set it manually.

You also need to know that Expires must be in the form Wdy, DD Mon YYYY HH:MM:SS GMT after the RFC-2616 Full Date section ( more information ).

In Java, you can do it like this:

 public void respond(HttpServletRequest req, HttpServletResponse resp) { int expiration = 3600; StringBuilder cookie = new StringBuilder("TestCookie=xyz; "); DateFormat df = new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss 'GMT'", Locale.US); Calendar cal = Calendar.getInstance(); cal.add(Calendar.SECOND, 3600); cookie.append("Expires=" + df.format(cal.getTime()) + "; "); cookie.append("Domain=; "); cookie.append("Version=0; "); cookie.append("Path=/; "); cookie.append("Max-Age=" + expiration + "; "); cookie.append("Secure; "); resp.setHeader("Set-Cookie", cookie.toString()); } 
+1
source share

All Articles