如何更新 JSP 中现有的 cookie?
我有一个 cookie myCookie
,其中包含哈希值。该 cookie 设置为一年后过期,路径为“/”。我需要用新的哈希值更新这个 cookie。当我的 JSP 脚本加载时,我会像这样检索 cookie:
Cookie[] cookies = request.getCookies();
Cookie myCookie = null;
for (int i = 0; i < cookies.length; i += 1) {
if (cookies[i].getName().equals("myCookie")) {
myCookie = cookies[i];
break;
}
}
在确定 cookie 的值需要更新后,我执行以下操作来更新它:
myCookie.setValue("my new value");
response.addCookie(myCookie);
检查结果,我现在有两个 myCookie:具有正确到期日期和路径的原始版本,以及旧的无效值;以及一个名为“myCookie”的新 cookie,该 cookie 在会话结束时过期,具有正确的值和 JSP 文档的路径。
如果我这样做:
myCookie.setValue("my new value");
myCookie.setPath(myCookie.getPath());
myCookie.setMaxAge(myCookie.getMaxAge());
response.addCookies(myCookie);
同样的事情就会发生。我得到两个具有相同名称和不同属性的 cookie。
Cookie 对象是否不保留检索时的属性?我怎样才能更新这个cookie?
注意:我不想修改路径或到期日期。我只想更新已经设置的cookie的值。
I have a cookie, myCookie
, that contains a hash value. This cookie is set to expire in one year and has a path of '/'. I need to update this cookie with a new hash value. When my JSP script is loaded I retrieve the cookie like so:
Cookie[] cookies = request.getCookies();
Cookie myCookie = null;
for (int i = 0; i < cookies.length; i += 1) {
if (cookies[i].getName().equals("myCookie")) {
myCookie = cookies[i];
break;
}
}
After determining that the value of the cookie needs to be updated, I do the following to update it:
myCookie.setValue("my new value");
response.addCookie(myCookie);
Examining the results, I now have two instances of myCookie
: the original version with the correct expiration date and path, and the old, invalid, value; and a new cookie named "myCookie" that expires at the end of the session, with the correct value, and a path of the JSP document.
If I do:
myCookie.setValue("my new value");
myCookie.setPath(myCookie.getPath());
myCookie.setMaxAge(myCookie.getMaxAge());
response.addCookies(myCookie);
The same thing happens. I get two cookies with the same name and different properties.
Does a Cookie object not retain the properties from when it was retrieved? How can I update this cookie?
Note: I do not want to modify the path or the expiration date. I only want to update the value of the already set cookie.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
根据 RFC 2965 第 3.3.4 节,用户代理不将过期信息包含在发送到服务器的 cookie 标头。因此,无法在保留最初仅根据与 cookie 关联的信息设置的到期日期的同时更新现有 cookie 的值。
所以这个问题的答案是:你不能那样做。
Per section 3.3.4 of RFC 2965, the user agent does not include the expiration information in the cookie header that is sent to the server. Therefore, there is no way to update an existing cookie's value while retaining the expiration date that was initially set based solely on the information associated with the cookie.
So the answer to this question is: you can't do that.
只需设置路径,例如:
这应该覆盖旧的 cookie 值。
Just set the path, ex:
This should overwrite the old cookie value.
如果您在 JSP 中操作 cookie,您需要注意的一件事是响应是否已经提交。一旦内容写入输出流,向响应添加 cookie 是徒劳的。
If you are manipulating cookies from within a JSP, one thing you will need to watch out for is whether or not the response has already been committed. Once content is written to the output stream, adding a cookie to the response is futile.
ServletResponseWrapper.isCommitted()
如果新 cookie 不包含相同的名称、路径和域,您可以通过将 MaxAge 设置为 (0) http://download.oracle.com/javaee/1.3/api/javax/servlet/http/Cookie.html#setMaxAge(int< /a>)
You can delete the old cookie, if the new one does not contain the same name, path, and domain by setting MaxAge to (0) http://download.oracle.com/javaee/1.3/api/javax/servlet/http/Cookie.html#setMaxAge(int)
上面的代码可以帮助您添加 cookie 并更新 cookie 值
The Above code can help you for ADD a cookie and UPDATE the cookie value