wood burning stoves 2.0*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Question regarding : cookie.getMaxAge() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Question regarding : cookie.getMaxAge()" Watch "Question regarding : cookie.getMaxAge()" New topic
Author

Question regarding : cookie.getMaxAge()

Khushbu Ghodasara
Ranch Hand

Joined: May 09, 2007
Posts: 35
Servlet that creates and sets the cookie :



and the servlet that gets the cookie :



Here, I'm setting the maximum age for the cookie as 30*60 (30 minutes) but when I reach the second code , where I'm retrieving the cookie info, it gives me the maximum age as -1, that means cookie is not being stored.

I am not able to understand the output. What am I missing here?
[ July 13, 2008: Message edited by: Khushbu Ghodasara ]
Dee Brown
Ranch Hand

Joined: Jun 14, 2008
Posts: 94
In your code you state, "Cookie [] cookies = request.getKookies();", so the code cannot compile. Can you supply the actual code that is being used?
Khushbu Ghodasara
Ranch Hand

Joined: May 09, 2007
Posts: 35
This is the actual code, but here it is not allowed to write getKookie() (read first K as C), due to security reasons, so they asked me to replace C with K in that particular method.
Dee Brown
Ranch Hand

Joined: Jun 14, 2008
Posts: 94
According to the api for the cookie class, "The browser returns cookies to the servlet by adding fields to HTTP request headers."

An associated rfc (2109) states:

4.3.4 Sending Cookies to the Origin Server

When it sends a request to an origin server, the user agent sends a
Cookie request header to the origin server...

The syntax for the header is:

cookie = "Cookie:" cookie-version
1*((";" | ",") cookie-value)
cookie-value = NAME "=" VALUE [";" path] [";" domain]
cookie-version = "$Version" "=" value
NAME = attr
VALUE = value
path = "$Path" "=" value
domain = "$Domain" "=" value

It would therefore appear the maxAge value you are retrieving is one the container supplies when it instantiates a cookie it receives from the client. Since the setting of maxAge is the setting of a value that is used by the client, the fact that the "-1" is retrieved is not all that important.

You can verify that the maxAge is not in the request header using the Live HttpHeaders plugin/addon in the Firefox browser.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question regarding : cookie.getMaxAge()
 
Similar Threads
JSp with Cookies HFSJ pg 386
Cookie
HTML display
Testing Cookies
Persistent Cookies with Servlets