Tomcat:Cookie 长度

发布于 2024-10-12 04:12:31 字数 894 浏览 1 评论 0原文

我正在为我的应用程序创建并附加一个烹饪,其中包含一些有用的信息。

但此后将无法访问此信息。

问题是,当我尝试附加太多信息时,我的 cookie 不会被转发。当我尝试附加一些信息时就可以了。

我的问题:

  1. cookie 值的最大长度是多少?
  2. 如果这是一个错误,Tomcat 5.5 有什么解决方法吗?

我使用以下代码创建我的 cookie:

Cookie cookie = new Cookie(key, value);
cookie.setPath("/");
response.addCookie(cookie);

为了检索我的 cookie,我使用以下代码:

private static String getCookieValue(HttpServletRequest request, String name) {
    boolean found = false;
    String result = null;
    Cookie[] cookies = request.getCookies();
    if (cookies != null) {
        int i = 0;
        while (!found && i < cookies.length) {
            if (cookies[i].getName().equals(name)) {
                found = true;
                result = cookies[i].getValue();
            }
            i++;
        }
    }

    return (result);
}

I'm creating and attaching a cooking for my application with some useful information.

But this information is not accessible after that.

The problem is that when I try to attach too much information, my cookie is not passed forward. When I try to attach few information it's OK.

My questions:

  1. What is the max length for cookies values?
  2. If it's a bug, there is any workaround for Tomcat 5.5?

I'm creating my cookie using the following code:

Cookie cookie = new Cookie(key, value);
cookie.setPath("/");
response.addCookie(cookie);

And for retrieving my cookie, I'm using this code:

private static String getCookieValue(HttpServletRequest request, String name) {
    boolean found = false;
    String result = null;
    Cookie[] cookies = request.getCookies();
    if (cookies != null) {
        int i = 0;
        while (!found && i < cookies.length) {
            if (cookies[i].getName().equals(name)) {
                found = true;
                result = cookies[i].getValue();
            }
            i++;
        }
    }

    return (result);
}

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

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

发布评论

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

评论(1

依 靠 2024-10-19 04:12:31

关于 1)“cookie 值的最大长度是多少?”

没有强制执行的最大值,只是 RFC 2109 中的建议:

为特定目的或有限容量而创建的用户代理
设备应提供至少 20 个 4096 字节的 cookie,以确保
用户可以与基于会话的源服务器交互。

请记住,每次端点通信时都会传输 cookie。

至于在客户端存储较大的值,dojox.storage< /a> 进入了我的脑海(虽然这是 javascript,但它显示了他们解决这个普遍问题的方法)。它们抽象了在客户端内存储大量数据的各种不兼容的方法 - 例如,通过使用可用的

  • AirDBStorageProvider、
  • AirEncryptedLocalStorageProvider、
  • AirFileStorageProvider、
  • FlashStorageProvider、
  • GearsStorageProvider、
  • WhatWGStorageProvider 之一。

如果您可以控制客户端,则可以使用 localstorage,最多提供5MB的本地客户端空间。

最后一个想法:压缩或编码存储的数据,或者将其分割,以便服务器可以接管一些实际值,而您只在客户端中存储密钥(这反过来会增加更多的连接开销)当然,整个事情)。

Concerning 1) "What is the max length for cookies values?"

There is no enforced maximum, just a recommendation in RFC 2109:

User agents created for specific purposes or for limited-capacity
devices should provide at least 20 cookies of 4096 bytes, to ensure
that the user can interact with a session-based origin server.

Keep in mind, that cookies will be transmitted each time the endpoints communicate.

As for storing larger values on the client side, dojox.storage came into my mind (although this is javascript, it shows their approach to this general problem). They abstract away the various incompatible ways to store large sets of data within the client - e.g. by using one of the available

  • AirDBStorageProvider,
  • AirEncryptedLocalStorageProvider,
  • AirFileStorageProvider,
  • FlashStorageProvider,
  • GearsStorageProvider,
  • WhatWGStorageProvider.

If you have control over the clients, you could go the HTML5 route with localstorage, which provides a maximum of 5MB of local client space.

And one final idea: Compress or encode the stored data, or maybe split it up, so that the server can take over some of the actual values, while you only store keys in the client (which in turn would add more connection overhead to the whole thing, of course).

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