如何更新 JSP 中现有的 cookie?

发布于 2024-10-19 00:40:27 字数 984 浏览 5 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

不寐倦长更 2024-10-26 00:40:27

根据 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.

金橙橙 2024-10-26 00:40:27

只需设置路径,例如:

cookie.setPath("/");

这应该覆盖旧的 cookie 值。

Just set the path, ex:

cookie.setPath("/");

This should overwrite the old cookie value.

网白 2024-10-26 00:40:27

如果您在 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()

风追烟花雨 2024-10-26 00:40:27

如果新 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)

挽心 2024-10-26 00:40:27
def member = SecUser.get(userService.currentUser().id)
    def cookies = request.getCookies()
    def cookie;
    def sum = 0;
    def cookieSum = 0;
    def cookieItems;
    for(def i=0; i<cookies.size(); i++){
        if (cookies[i].name == 'c17'){
                cookie = cookies[i]
                cookieItems = cookie.value.split('-')
                println "cookieItems......."+cookieItems
                if(params.itemId != null){
                    for(def j=0; j<cookieItems.size(); j++){
                        def oldItem = cookieItems[j].split('\\|')[0]
                        if(params.itemId != oldItem){
                            sum = sum + 1
                        }
                    }//Below code for Update your cookie value
                if(sum == cookieItems.size()){
                    cookie.value = cookie.value +"-"+params.itemId+"|"+member.id
                    def b = cookie.value
                    cookie.setValue(b);
                    response.addCookie(cookie);

                }
                }
                break
          }
          else{
             cookieSum = cookieSum + 1
          }

     }
    if ((cookieSum) == cookies.size()){
        // Here ADD new cookie........
         def a = params.itemId+"|"+member.id
         cookie = new Cookie('c17',a.toString())
         cookie.path = '/'
         response.addCookie(cookie)
    }

上面的代码可以帮助您添加 cookie 并更新 cookie 值

def member = SecUser.get(userService.currentUser().id)
    def cookies = request.getCookies()
    def cookie;
    def sum = 0;
    def cookieSum = 0;
    def cookieItems;
    for(def i=0; i<cookies.size(); i++){
        if (cookies[i].name == 'c17'){
                cookie = cookies[i]
                cookieItems = cookie.value.split('-')
                println "cookieItems......."+cookieItems
                if(params.itemId != null){
                    for(def j=0; j<cookieItems.size(); j++){
                        def oldItem = cookieItems[j].split('\\|')[0]
                        if(params.itemId != oldItem){
                            sum = sum + 1
                        }
                    }//Below code for Update your cookie value
                if(sum == cookieItems.size()){
                    cookie.value = cookie.value +"-"+params.itemId+"|"+member.id
                    def b = cookie.value
                    cookie.setValue(b);
                    response.addCookie(cookie);

                }
                }
                break
          }
          else{
             cookieSum = cookieSum + 1
          }

     }
    if ((cookieSum) == cookies.size()){
        // Here ADD new cookie........
         def a = params.itemId+"|"+member.id
         cookie = new Cookie('c17',a.toString())
         cookie.path = '/'
         response.addCookie(cookie)
    }

The Above code can help you for ADD a cookie and UPDATE the cookie value

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文