开发者

why cookie.getMaxAge() = -1?

开发者 https://www.devze.com 2023-01-04 02:05 出处:网络
i have a test like this : cookie.jsp: <html> <head> </head> <body> <% String cookieName=\"SNS\";

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>
</hea开发者_StackOverflow社区d>  
<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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消