从 Java 设置持久 cookie 在 IE 中不起作用

发布于 2024-08-10 10:01:34 字数 769 浏览 4 评论 0原文

所有,

虽然我在论坛上看到了相关主题,但我没有看到这个问题的明确解决方案。 我正在尝试设置 javax.servlet.http.Cookie 的过期时间(以便它在浏览器会话中持续存在)。代码:

public void respond(HttpServletRequest req, HttpServletResponse resp) {
    int expiration = 3600;
    Cookie cookie = new Cookie("TestCookie", "xyz");
    cookie.setDomain("");
    cookie.setVersion(0);
    cookie.setPath("/");
    cookie.setMaxAge(expiration);
    cookie.setSecure(false);
    resp.addCookie(cookie);
}

当我签入 IE 开发者工具时,我没有看到此 cookie 被设置。在互联网上搜索给我的线索是IE不考虑Max-Age,而只适用于Expires。如果这对 IE 不起作用,那么是否有一种行之有效的方法来设置持久 cookie 的 HTTP 响应标头,以便它适用于 IE?

PS:这在所有其他浏览器上都可以正常工作。

我尝试为具有过期属性的 cookie 创建一个字符串。 IE 成功创建了它,但它丢失了域(默认 - “”)并显示“.com”,并将其转换为会话 cookie 而不是持久 cookie。这在所有其他浏览器上再次运行良好。

请帮忙。 谢谢。

All,

Although I see related topics on the forum, but I don't see a clear solution on this issue.
I am trying to set a javax.servlet.http.Cookie with an expiration time (so that it persists across browser sessions). Code:

public void respond(HttpServletRequest req, HttpServletResponse resp) {
    int expiration = 3600;
    Cookie cookie = new Cookie("TestCookie", "xyz");
    cookie.setDomain("");
    cookie.setVersion(0);
    cookie.setPath("/");
    cookie.setMaxAge(expiration);
    cookie.setSecure(false);
    resp.addCookie(cookie);
}

I don't see this cookie being set when I check in IE developer tools. Searching on the internet gave me clues that IE doesn't consider Max-Age, but only works with Expires. If this does not work for IE, then is there a proven way of setting the HTTP response headers for a persistent cookie so that it works for IE?

PS: This works fine on all other browsers.

I tried creating a string for the cookie having expires attribute. IE succeeded in creating it, but it lost the domain (default - "") and showed ".com" and turned it into a session cookie instead of a persistent cookie. This again works fine on all other browsers.

Please help.
Thanks.

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

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

发布评论

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

评论(3

静谧 2024-08-17 10:01:34

使用 IE9,我发现需要 HttpOnly 属性才能让它在后续帖子中回显 cookie 值,例如:

Set-Cookie: autologCk1=ABCD; Path=/autolog/; HttpOnly

Working with IE9, I found that it was the HttpOnly attribute that was required in order to get it to echo the cookie value on subsequent posts, e.g:

Set-Cookie: autologCk1=ABCD; Path=/autolog/; HttpOnly
淡看悲欢离合 2024-08-17 10:01:34

答案就在 来自 IE 中 servlet 的持久 cookie

您的情况可能是同一问题的不同形式:也就是说,通过在域前添加“.”前缀。 (我很确定这是版本 1 的 cookie 功能),Java 堆栈中的某些内容决定它是版本 1 的 cookie(IE 无法识别且不保留,甚至是 IE8)并发送该 cookie 格式。

或者,正如该答案所暗示的那样,您的 cookie 值中是否包含无法识别的字符?

The answer is at Persistent cookies from a servlet in IE.

Your case may be a different flavour of the same issue: that is, by prefixing the domain with a "." (which I'm pretty sure is a version 1 cookie feature), something in the Java stack decides it's a version 1 cookie (unrecognized and not persisted by IE, even IE8) and sends that cookie format.

Or, as that answer suggests, is there something in your cookie value that contains an unrecognized character?

墨小墨 2024-08-17 10:01:34

由于javax.servlet.http.Cookie不允许您为cookie设置Expires属性,因此您应该手动设置它。

您还需要知道 Expires 必须按照 RFC-2616Wdy, DD Mon YYYY HH:MM:SS GMT 的形式指定完整日期部分(更多信息)。

在Java中你可以这样做:

public void respond(HttpServletRequest req, HttpServletResponse resp) {
    int expiration = 3600;
    StringBuilder cookie = new StringBuilder("TestCookie=xyz; ");

    DateFormat df = new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss 'GMT'", Locale.US);
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.SECOND, 3600);
    cookie.append("Expires=" + df.format(cal.getTime()) + "; ");

    cookie.append("Domain=; ");
    cookie.append("Version=0; ");
    cookie.append("Path=/; ");
    cookie.append("Max-Age=" + expiration + "; ");
    cookie.append("Secure; ");
    resp.setHeader("Set-Cookie", cookie.toString());
}

As javax.servlet.http.Cookie does not allow you to set Expires attribute to the cookie, you should set it manually.

You also need to know that Expires must be specified in the form of Wdy, DD Mon YYYY HH:MM:SS GMT following RFC-2616 Full Date section (more info).

In Java you can do it this way:

public void respond(HttpServletRequest req, HttpServletResponse resp) {
    int expiration = 3600;
    StringBuilder cookie = new StringBuilder("TestCookie=xyz; ");

    DateFormat df = new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss 'GMT'", Locale.US);
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.SECOND, 3600);
    cookie.append("Expires=" + df.format(cal.getTime()) + "; ");

    cookie.append("Domain=; ");
    cookie.append("Version=0; ");
    cookie.append("Path=/; ");
    cookie.append("Max-Age=" + expiration + "; ");
    cookie.append("Secure; ");
    resp.setHeader("Set-Cookie", cookie.toString());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文