Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Persistent cookies from a servlet in IE

I have a cookie which is generated from a servlet and that I would like to be persistent - that is, set the cookie, close down IE, start it back up, and still be able to read the cookie. The code that I'm using is the following:

HttpServletResponse response = 
    (HttpServletResponse) FacesContext.getCurrentInstance()
    .getExternalContext().getResponse();

Cookie cookie = new Cookie("someKey", "someValue");
cookie.setMaxAge(7 * 24 * 60 * 60);
response.addCookie(cookie);

This works great in firefox, but in IE 6/7, the cookie is not saved between browser restarts. I've checked everything that I can think of in my settings, but can't figure out what would be causing the cookie to be deleted. As far as I know, calling setMaxAge with a positive number makes the cookie persistent. Any ideas why this would be going wrong?

Edit

I have verified, using the more info trick suggested by Olaf, that the cookie is attempting to be set as a session cookie, not a persistent cookie; the max age is set to "end of session". So it doesn't seem like the max age is being set for IE - I have verified that in Firefox, the max age is set correctly. I still have no idea what's going on.

like image 695
Matt McMinn Avatar asked Dec 11 '08 22:12

Matt McMinn


People also ask

How are persistent cookies stored?

Persistent Cookies are stored on a user's device i.e. on placed on the device's hard disk.

What are persistent cookies called?

A persistent cookie is a data file capable of providing websites with user preferences, settings and information for future visits. Persistent cookies provide convenient and rapid access to familiar objects, which enhances the user experience (UX). A persistent cookie is also known as a stored or permanent cookie.

What is a persistent cookie and how it is different from the temporary cookie?

Temporary cookies get deleted when you close the browser. No cookie is forever. "If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes)." It's "persistent" because you specify an expire date.


1 Answers

I know nothing of Java or servlets, but IE will only persist a cookie if it has an Expires date, setting max-age is not sufficient, IE will continue to treat it as a session cookie.

like image 189
Tom Evans Avatar answered Oct 21 '22 12:10

Tom Evans