Tomcat:Cookie 长度
我正在为我的应用程序创建并附加一个烹饪,其中包含一些有用的信息。
但此后将无法访问此信息。
问题是,当我尝试附加太多信息时,我的 cookie 不会被转发。当我尝试附加一些信息时就可以了。
我的问题:
- cookie 值的最大长度是多少?
- 如果这是一个错误,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:
- 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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关于 1)“cookie 值的最大长度是多少?”
没有强制执行的最大值,只是 RFC 2109 中的建议:
请记住,每次端点通信时都会传输 cookie。
至于在客户端存储较大的值,
dojox.storage
< /a> 进入了我的脑海(虽然这是 javascript,但它显示了他们解决这个普遍问题的方法)。它们抽象了在客户端内存储大量数据的各种不兼容的方法 - 例如,通过使用可用的如果您可以控制客户端,则可以使用 localstorage,最多提供5MB的本地客户端空间。
最后一个想法:压缩或编码存储的数据,或者将其分割,以便服务器可以接管一些实际值,而您只在客户端中存储密钥(这反过来会增加更多的连接开销)当然,整个事情)。
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.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 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).