tomcat7中cookie写入中文引发Control character in cookie value or attribute异常

java.lang.IllegalArgumentException: Control character in cookie value or attribute.   
    at org.apache.tomcat.util.http.CookieSupport.isV0Separator(CookieSupport.java:155)   
    at org.apache.tomcat.util.http.Cookies.processCookieHeader(Cookies.java:323)   
    at org.apache.tomcat.util.http.Cookies.processCookies(Cookies.java:157)   
    at org.apache.tomcat.util.http.Cookies.getCookieCount(Cookies.java:98)   
    at org.apache.catalina.connector.CoyoteAdapter.parseSessionCookiesId(CoyoteAdapter.java:913)   
    at org.apache.catalina.connector.CoyoteAdapter.postParseRequest(CoyoteAdapter.java:683)   
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:400)   
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:964)   
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)   
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:304)   
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)   
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)   

    at java.lang.Thread.run(Thread.java:662)  


出现上面的错误主要是cookie中存在中文或特殊符号造成的,tomcat7不支持导致。

 
 
解决办法:
1、先删除本地cookie,否则项目无法访问。
tomcat7中cookie写入中文引发Control character in cookie value or attribute异常_第1张图片
 
 
 
 
2、然后调整编码
原因是有关中文编码的问题,中文采用的是unicode编码,而英文采用的是ASCII编码,所以当COOkie保存中文的时候需要对中文进行编码,而且从Cookie中取出内容的时候也要进行解码,编码和解码可以使用
URLEncoder.encode(name, "utf-8");

URLDecoder.decode(cookies[i].getName(),"utf-8")

你可能感兴趣的:(tomcat7中cookie写入中文引发Control character in cookie value or attribute异常)