Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

setMaxAge( ) not working for the cookie...do you know what's wrong?

843836Jan 4 2005 — edited Nov 17 2009
I've just started working with and testing cookies. So I created a very simple servlet that upon viewing in a browser it will create one. A link is supplied which will go to a simple jsp page that shows the attributes of the cookie itself.

I couldn't help but notice that even though I set the
setMaxAge()
attribute to 60*60*24*180 (because as you know the parameter must be an integer type and in seconds. So this will equate to 180 days.) that the maxAge value when displayed in my jsp page is -1. A negative number means the cookie is not stored persistently and will be deleted upon exiting the browser.

I'm using Tomcat version 4 for my localhost server.
Below are snippets of the code for the servlet and the jsp pages.
//....Servlet Snippet
           Cookie status = new Cookie("status","visited");
           status.setMaxAge((60*60*24*180)); //180 days in seconds
           response.addCookie(status);
...//

//.....jsp Snippet
   Cookie[] visits = request.getCookies();      
   
   for(int x = 0; x < visits.length; x++) {
        out.println("Cookie name is : " + visits[x].getName());
        out.println("Cookie value is : " + visits[x].getValue());
        out.println("Cookie maxAge is: " + visits[x].getMaxAge());        
   }
....//
Can anyone explain why it shows a -1 value? I want this cookie to last 180 days, and not be deleted upon exiting the browser.
What am I doing wrong??
Any help is appreciated:)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 15 2009
Added on Jan 4 2005
17 comments
4,762 views