Reputation: 21
I am attempting to both set and get a cookie that has brackets in the name. I am able to do this in PHP, but reading back the cookie in Java isn't working for me. Here is the code to set the cookies in my JSP:
Cookie testCookie = new Cookie("test[cuid]", "test1");
Cookie testCookie2 = new Cookie("test", "test2");
testCookie.setMaxAge(60);
testCookie2.setMaxAge(60);
response.addCookie(testCookie);
response.addCookie(testCookie2);
Here is the code to read the cookies:
Cookie[] cookies = request.getCookies();
if(cookies != null){
out.println("# Cookies: " + cookies.length + "<br/>");
for (Cookie cookie : cookies){
out.print("Name: " + cookie.getName( ) + " - ");
out.print("Value: " + cookie.getValue( ) + "<br/>");
}
}
else {
out.println("No cookies found");
}
Results:
# Cookies: 2
Name: JSESSIONID - Value: DD299706C704FE80BC99C10EA2C6E9F9
Name: test - Value: test2
I can see in Chrome that both cookies are getting set with the correct names and values. Only the "test" cookie is getting displayed when I loop through the cookies; the "test[cuid]" cookie does not appear. I've found a few instances of others having issues with Tomcat throwing out cookies with special characters, but I haven't found a solution for it. As far as I can tell, RFC 2109 does not explicitly disallow brackets in version 1 cookie names.
Versions:
Edit: I should note that the purpose here is to retrieve a cookie set by another web service, so any solution that involves changing the cookie name won't be viable.
Upvotes: 0
Views: 462
Reputation: 21
I still don't know how to force Tomcat to accept my cookie, but I at least have a workaround for now. I call
String cookies = request.getHeader("Cookie");
and then parse it myself, i.e. tokenizing it by semi-colon ";" and then by equals "=".
Upvotes: 1