Illegalargumentexception: control character in cookie value or attribute tomcat7

It is disgusting to encounter this exception. In Tomcat7 environment, this exception will be raised if the user’s name appears in Chinese when logging in. After the exception is caused, the user who cannot log in normally needs to delete the browser’s cookie information. Later, I checked the Internet and found out that the problem was Chinese transcoding. The abnormal information is as follows:

java.lang.IllegalArgumentException: Control character in cookie value or attribute.     
    at org.apache.tomcat.util.http.CookieSupport.isV0Separator(     
    at org.apache.tomcat.util.http.Cookies.processCookieHeader(     
    at org.apache.tomcat.util.http.Cookies.processCookies(     
    at org.apache.tomcat.util.http.Cookies.getCookieCount(     
    at org.apache.catalina.connector.CoyoteAdapter.parseSessionCookiesId(     
    at org.apache.catalina.connector.CoyoteAdapter.postParseRequest(     
    at org.apache.catalina.connector.CoyoteAdapter.service(     
    at org.apache.coyote.http11.AbstractHttp11Processor.process(     
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(     
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(     
    at java.util.concurrent.ThreadPoolExecutor$     

Cookie is encoded by Unicode in Chinese and ASCII in English when storing the value, so the data needs to be transcoded when storing the Chinese value, and the data also needs to be transcoded when storing the value.
There are two solutions, one is a Cookie written in Java, and the other is a Cookie written in JavaScript.
The solution is as follows:

Encode: URLEncoder. Encode (name, “UTF-8”);
Decode.decode (name, “UTF-8”);
Decode.decode (name, “UTF-8”);


Code: the escape (name);/encodeURI(name);
decode: unescape(name);/decodeURI(name);

The Servlet setting cookies to view the article:

Read More: