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