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:)