Cookie Version in J2EE

Source: Internet
Author: User

Cookie Version in J2EE


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 '' 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

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.

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.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.