tags:

views:

49

answers:

2

i have a test like this :

cookie.jsp:

<html>  
<head>
</head>  
<body>  
<%  
String cookieName="SNS";  
Cookie cookie=new Cookie(cookieName, "maxAgeTest");  
cookie.setMaxAge(60*60);  
response.addCookie(cookie);  
%>  
</body>  
</html>

and read.jsp is :

<html>  
<head>
</head>  
<body>  
<table border=1>  
<tr><td>Name</td><td>value</td></tr>  
<%  
Cookie cookies[]=request.getCookies();  
Cookie sCookie=null;  
String svalue=null;  
String sname=null;  
int sage ; 
for(int i=0;i<cookies.length;i++)  
{  
sCookie=cookies[i];  
svalue=sCookie.getValue();  
sname=sCookie.getName();  
sage=sCookie.getMaxAge();
%>  
<tr><td><%=sname%></td><td><%=svalue%></td><td><%=sage%></td></tr>  
<%  
}  
%>  
</table>
</body>  
</html> 

but the result is :

Name value maxAge

JSESSIONID DB3561A47B37FCA8CA25EA04B80A26C7 -1

SNS maxAgeTest -1

why the maxAge is -1 ?

and t test IE8,Chrome5,Safari ,the result same

+2  A: 

Because you haven't set the MaxAge (using setMaxAge). So it will have the default value (-1). This means that the cookie will persist until browser shutdown

Sander Pham
cookie.setMaxAge(60*60);i have set the value ...
+1  A: 

you ain't gonna get it. the browser only sends back name and value of the cookies, no other information. that's in the cookie spec.

irreputable