Cookie Version in J2EE

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.