<big><code>cookie.getMaxAge()</code></big> always returns -1 (regardless of real expiry).
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Garrett_Smith
Posted On:   Sunday, December 1, 2002 11:24 AM

Hello, I would like to know why my cookies are always "-1" expiry. The browser preferences allows me to inspect my cookies. I get the same result using mozilla 1.2 and mac IE 5.2. Here is my sample code: <%@ page import="java.util.*,java.io.*" %> <%! public static void printCookies(JspWriter out, Cookie[] cookies) throws Exception{ out.println("Printing cookies"); for(int i = 0; i < cookies.length; i++){ out.println("Cookie "+i); out.println("name: "+ cookies[i].getName()); out.println("value: "+ cookies[i].getValue());    More>>

Hello,


I would like to know why my cookies are always "-1" expiry. The browser preferences allows me to inspect my cookies. I get the same result using mozilla 1.2 and mac IE 5.2.



Here is my sample code:


			
				
<%@ page import="java.util.*,java.io.*" %>
<%!
public static void printCookies(JspWriter out, Cookie[] cookies) throws Exception{

out.println("Printing cookies");

for(int i = 0; i < cookies.length; i++){

out.println("Cookie "+i);
out.println("name: "+ cookies[i].getName());
out.println("value: "+ cookies[i].getValue());
out.println("domain: "+ cookies[i].getDomain());
out.println("path: "+ cookies[i].getPath());
out.println("secure: "+ cookies[i].getSecure());
out.println("max-age: "+ cookies[i].getMaxAge());

}
out.println("Done printing cookies");

}

%>

				
					
<% printCookies(out, request.getCookies()); %>



Result



			

Printing cookies

Cookie 0
name: activeTabsindex.jsp?null
value:
domain: null
path: null
secure: false
max-age: -1


Cookie 1
name: JSESSIONID
value: 5BF9757F1CC1B9FC34EDA45BA5460B8E
domain: null
path: null
secure: false
max-age: -1


Cookie 2
name: activeTabsindex.jsp
value:
domain: null
path: null
secure: false
max-age: -1


Cookie 3
name: foobar
value: bar
domain: null
path: null
secure: false
max-age: -1



Done printing cookies

How can we know if the cookie is not really a temp cookie?



Proof: I quit the browser (file > quit), launch the browser, and the cookies are all printed out (and with -1 for max age)!



I made a separate test-case creation cookie page. Please try the test page.



			
				
<%@ page import="java.util.* " %>
<%!

public static Cookie addOrModifyCookie(String name,
String value,
String path,
String domain,
int expires,
Cookie[] cookies,
HttpServletResponse response){

Cookie foundCookie = null;

if(cookies != null)
for(int i = 0; i < cookies.length; i++){

if(cookies[i].getName().equals(name)){
foundCookie = cookies[i];

cookies[i].setValue(value);
if(path != null)
cookies[i].setPath(path);
if(domain != null)
cookies[i].setDomain(domain);
cookies[i].setMaxAge(expires);
break;
}
}
if(foundCookie == null) {
foundCookie = new Cookie(name, value);
if(path != null)
foundCookie.setPath(path);
if(domain != null)
foundCookie.setDomain(domain);
foundCookie.setMaxAge(expires);
}

// put it down here, where it can be
response.addCookie(foundCookie);

return foundCookie;

}



%>


<%

final String DOMAIN =
request.getRequestURL().toString().indexOf("127.0.0.1") > 5 ?
null : ".dhtmlkitchen.com";

Cookie c = addOrModifyCookie("foobar",
"bar",
"/",
DOMAIN,
60 * 2,
request.getCookies(),
response);

out.print(c.getMaxAge());%>



Why does cookie.getMaxAge() always return -1?    <<Less

Re: <big><code>cookie.getMaxAge()</code></big> always returns -1 (regardless of real expiry).

Posted By:   Hitoshi_Gosen  
Posted On:   Sunday, December 1, 2002 04:29 PM

By default, the maxAge is set to a negative number.
You should set the maximum age with setMaxAge(int seconds) with a positive number to set your own expiry right after creating the cookie. Use getMaxAge() right after setting the expiry, because once the cookie itself is set to the client browser, the getMaxAge() doesn't perform like the way you are intending.

Re: <big><code>cookie.getMaxAge()</code></big> always returns -1 (regardless of real expiry).

Posted By:   Garrett_Smith  
Posted On:   Sunday, December 1, 2002 11:47 AM

According to this: http://www.caucho.com/support/resin-interest/0208/0068.html

It is not possible.



Is it true? The Javadoc makes no mention of this.
About | Sitemap | Contact