0

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);
}
4

1 回答 1

1

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).

于 2011-01-17T18:20:59.243 回答