在 Java Servlet 中如何更改现有 cookie 的值?

发布于 2024-12-05 09:06:05 字数 100 浏览 0 评论 0原文

在 Java Servlet 中如何更改现有 cookie 的值? HttpServletResponse中有addCookie方法,但没有deleteCookie或editCookie

In a Java Servlet how can I change the value of an existing cookie? There is an addCookie method, but no deleteCookie or editCookie in HttpServletResponse

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

凶凌 2024-12-12 09:06:05

那些确实不存在。只需自己创建实用方法即可实现此目的。特别是得到想要的cookie是相当臃肿的。例如,

public final class Servlets {

    private Servlets() {}

    public static Cookie getCookie(HttpServletRequest request, String name) {
        if (request.getCookies() != null) {
            for (Cookie cookie : request.getCookies()) {
                if (cookie.getName().equals(name)) {
                    return cookie;
                }
            }
        }

        return null;
    }

}

要编辑 cookie,请设置其值,然后将其添加到响应中:

Cookie cookie = Servlets.getCookie(request, "foo");

if (cookie != null) {
    cookie.setValue(newValue);
    response.addCookie(cookie);
}

如有必要,请设置 maxage、路径和域(如果它们与默认值不同)。即客户端不会发回此信息。

要删除 cookie,请将最大期限设置为 0(最好将值设置为 null):

Cookie cookie = Servlets.getCookie(request, "foo");

if (cookie != null) {
    cookie.setMaxAge(0);
    cookie.setValue(null);
    response.addCookie(cookie);
}

如果需要,请设置路径和域(如果它们与默认值不同) 。即客户端不会发回此信息。

Those indeed don't exist. Just create utility methods yourself which do that. Particularly getting the desired cookie is quite bloated. E.g.

public final class Servlets {

    private Servlets() {}

    public static Cookie getCookie(HttpServletRequest request, String name) {
        if (request.getCookies() != null) {
            for (Cookie cookie : request.getCookies()) {
                if (cookie.getName().equals(name)) {
                    return cookie;
                }
            }
        }

        return null;
    }

}

To edit a cookie, set its value and then add it to the response:

Cookie cookie = Servlets.getCookie(request, "foo");

if (cookie != null) {
    cookie.setValue(newValue);
    response.addCookie(cookie);
}

Set if necessary the maxage, path and domain as well if they differs from your default. The client namely doesn't send this information back.

To delete a cookie, set the max age to 0 (and preferably also the value to null):

Cookie cookie = Servlets.getCookie(request, "foo");

if (cookie != null) {
    cookie.setMaxAge(0);
    cookie.setValue(null);
    response.addCookie(cookie);
}

Set if necessary the path and domain as well if they differs from your default. The client namely doesn't send this information back.

习惯成性 2024-12-12 09:06:05

这是来自 kodejava 的一个示例:

 public class ReadCookieExample extends HttpServlet {
 protected void doPost(HttpServletRequest request, HttpServletResponse   response)         throws    ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter writer = response.getWriter();

    Cookie[] cookies = request.getCookies();
    for (int i = 0; i < cookies.length; i++) {
        writer.println("Name: " + cookies[i].getName() + "; Value: "                    +                   cookies[i].getValue());            
    }
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws    ServletException, IOException {
    doPost(request, response);
}

}

这将获取 cookie 列表,获取您想要的列表,而不是打印出值,而是执行类似于此的操作:

 cookie.setValue(String.valueOf(<new Value>));  
 cookie.setMaxAge(60*60*24*365);   
 cookie.setPath("/");   
 response.addCookie(cookie);  etc...

HTH,

James

Here is an example from kodejava:

 public class ReadCookieExample extends HttpServlet {
 protected void doPost(HttpServletRequest request, HttpServletResponse   response)         throws    ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter writer = response.getWriter();

    Cookie[] cookies = request.getCookies();
    for (int i = 0; i < cookies.length; i++) {
        writer.println("Name: " + cookies[i].getName() + "; Value: "                    +                   cookies[i].getValue());            
    }
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws    ServletException, IOException {
    doPost(request, response);
}

}

That will get the cookies list, get the one you want and instead of printing out values, do something similar to this:

 cookie.setValue(String.valueOf(<new Value>));  
 cookie.setMaxAge(60*60*24*365);   
 cookie.setPath("/");   
 response.addCookie(cookie);  etc...

HTH,

James

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