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:
- What is the max length for cookies values?
- 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);
}
Concerning 1) “What is the max length for cookies values?”
There is no enforced maximum, just a recommendation in RFC 2109:
Keep in mind, that cookies will be transmitted each time the endpoints communicate.
As for storing larger values on the client side,
dojox.storagecame 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 availableIf 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).