JSP中cookie.getMaxAge()怎么总是得到-1呢?
Java code//设置Cookie
Cookie cookie = new Cookie("username","guest");
cookie.setMaxAge(60*60*24);//设置过期时间
response.addCookie(cookie);
为什么我在Jsp中取出Cookie的时候,getMaxAge()的值为-1;
Jsp代码:
Java code
<% Cookie[] cookies = request.getCookies();
if(cookies != null) {
for(Cookie c:cookies){
%>
cookieName:<%=c.getName()%><br>
cookieValue:<%=c.getValue()%><br>
cookieMaxAge:<%=c.getMaxAge()%><br>
<%
}
}
%>
Jsp显示结果:
cookieName:username
cookieValue:guest
cookieMaxAge:-1
怎样才能得到Cookie过期时间! --------------------编程问答-------------------- 把cookie的路径设置一下。设成你应用名的根目录试试 --------------------编程问答-------------------- -1应该为不过期 --------------------编程问答-------------------- cookie的时间是毫秒吧 你有没有很长时间没有处理呢, 还有就是你response.addCookie(cookie);
后 马上取出来看一下咯 --------------------编程问答-------------------- -1表示Cookie会在浏览器关闭时过期 --------------------编程问答-------------------- 你可以自己设过期时间呀 --------------------编程问答-------------------- 学习了 --------------------编程问答-------------------- mark --------------------编程问答--------------------
当时取出来就是-1
Cookie cookie = new Cookie("username","guest");
cookie.setMaxAge(60*60*24);//设置过期时间
response.addCookie(cookie);
Cookie[] cookies = request.getCookies();
if(cookies != null) {
for(Cookie c:cookies){
System.out.println(c.getName());
System.out.println(c.getMaxAge());
};
}else
{
}
--------------------编程问答-------------------- I don't know if this is off topic for this list but I will ask anyway. I
> written two java servlets which I am running on tomcat 4.0. One servlet
> creates cookies, the other displays details about them. One thing I have
> done is to use the setMaxAge method on a cookie object. But when I
> interrogate the cookie using getMaxAge in the other servlet the value is
> always -1 reagardless of what I originaly set it to. Having a value of -1
> means that the cookie gets to deleted as soon as the browser window is
> closed. This could be a problem with my browser settings (IE 5) or
> something with tomcat. I am pretty sure I am setting the value correctly in
> the java code.
>
The answer to this is in the cookie specification (don't recall which RFC
it is at the moment).
When you set up a cookie and send it, you can populate all of the
properties. However, when the browser returns the cookie with a
subsequent request, it *only* sends the value. Thus, there is no way toi
determine what the browser thinks the maximum age is; you will have to
remember what you told it in the first place.
哎,老外也没有说出个所以然来!好像说之传递了cookie的value值。
--------------------编程问答-------------------- --------------------编程问答-------------------- 那这算不算是不是一个BUG呢? --------------------编程问答-------------------- 这样设置肯定是生效了,但是这个函数getMaxAge()好像确实不能获取到正确值。呵呵,我感觉是bug(个人见解) --------------------编程问答-------------------- -1 indicating the cookie will persist until browser shutdown. --------------------编程问答--------------------
这个Cookie根本还没有送到客户端,所以request.getCookies来是旧的,所以MaxAge还是旧的。 --------------------编程问答-------------------- Cookie没有送到客户端,MaxAge还是旧的。 --------------------编程问答-------------------- cookie 的流程
new Cookie()->response ->client ->request -> getCookies 如果你设置的PATH正确的话,就能正确取到了 --------------------编程问答-------------------- 楼主,这个问题,你解决了么?他们的答案我表示没看懂啊?我遇到和你一样的问题了
--------------------编程问答-------------------- 工具——internet选项——退出浏览器时删除浏览记录前边的勾去掉 --------------------编程问答-------------------- cookie好像是以毫秒为单位的。
补充:Java , Web 开发