gpt4 book ai didi

java - 在 HTTPServlet doPost() 中将 maxage(0) 设置为 cookie 并且重定向不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:13:22 28 4
gpt4 key购买 nike

我正在做一些非常简单但不起作用的事情。

我有一个带有按钮的 html 页面,该按钮应该注销并删除登录 cookie,按下时,它/在 servlet 中运行此代码:

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
IOException {
deleteCookie(req, resp);

resp.sendRedirect("login");

}

private void deleteCookie(HttpServletRequest req, HttpServletResponse resp) {
Cookie[] cookies = req.getCookies();

if (cookies != null) {
for (Cookie cookie : cookies) {

if (CID_KEY_NAME.equals(cookie.getName())) {
cookie.setMaxAge(0); // Should make the browser delete the cookie
cookie.setValue(null);
resp.addCookie(cookie);

}
}
}
}

sendRedirect("login") 将我发送到负责登录的新 servlet,但是当我到达新 servlet 并检查上面的 cookie 是否存在时,我可以看到它存在,maxage 值为 -1

我做错了什么吗?

最佳答案

根据 HTTP Cookies: What's the difference between Max-age and Expires? ,至少 Internet Explorer 6、7 和 8 不支持 Max-Age,但支持 Expires。如果这是你的问题,那么我会尝试将这两个设置为:

Q. What if I set both expires and max-age in a cookie?

A. Every browser that supports max-age will ignore the expires regardless of it’s value, and likewise, Internet Explorer will ignore the max-age and just use expires.

该页面还有一个指向 cookie persistence test page 的链接您可能会觉得有用。

关于java - 在 HTTPServlet doPost() 中将 maxage(0) 设置为 cookie 并且重定向不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8562256/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com