Cookie Version in J2EE
Article: http://villadora.me/2014/05/06/cookie-version/
When processing the Cookie, it is found that the servlet request cannot be processed and the ":" character value in the cookie cannot be obtained.
Cookie[] cookies = request.getCookies();if (cookies != null) { for (Cookie cookie : cookies) { if (StringUtils.equalsIgnoreCase(cookie.getName(), name)) { value = cookie.getValue(); // if the value in cookie is 'http://example.com' then here it will get 'http' break; } }}
This is because currently there are two standards for Cookie, one is Version 0 (Netscape spec)
J2EE implementation description Cookie # setValue
12 |
With Version 0 cookies, values should not contain white space, brackets, parentheses, equals signs, commas, double quotes, slashes, question marks, at signs, colons, and semicolons. Empty values may not behave the same way on all browsers.` |
That is to say, Version 0 cannot contain characters such as space, arc, equal sign, comma, and double quotation marks.
Version 1 (RFC 2109 spec) is acceptable.
However, when javax. servlet. http. Cookie is implemented, Version 0 is used by default.
12 |
By default, cookies are created according to the Netscape cookie specification. The version can be changed with thesetVersion method. |
It seems that the default choice of container is also to use Version 0 without changing the version. Therefore, when the Cookie value contains ':', the content after colon cannot be read.
If there is no way to change the container and only use the default request, the temporary solution is to URLEncode when writing the cookie, and then perform URLDecode when reading the server.