tomcat使用cookies缓存的时候中文报错解决办法 java.lang.IllegalArgumentException: Control character in cookie value or attribute.
报错出现
java.lang.IllegalArgumentException: Control character in cookie value or attribute.
at org.apache.tomcat.util.http.CookieSupport.isHttpSeparator(CookieSupport.java:205)
at org.apache.tomcat.util.http.CookieSupport.isHttpToken(CookieSupport.java:229)
at org.apache.tomcat.util.http.ServerCookie.appendCookieValue(ServerCookie.java:185)
at org.apache.catalina.connector.Response.generateCookieString(Response.java:1054)
at org.apache.catalina.connector.Response.addCookie(Response.java:996)
at org.apache.catalina.connector.ResponseFacade.addCookie(ResponseFacade.java:380)
at cn.lonecloud.CookieServlet.doGet(CookieServlet.java:18)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:442)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1082)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:623)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
该报错是由于这个中文的编码的问题中文采用的是unicode编码,而英文采用的是ASCII编码,所以当COOkie保存中文的时候需要对中文进行编码,而且从Cookie中取出内容的时候也要进行解码,编码和解码可以使用
URLEncoder.encode(name, "utf-8");
URLDecoder.decode(cookies[i].getName(),"utf-8");
示例:
CookieServlet.java 生成cookies
1 package cn.lonecloud; 2 3 import java.io.IOException; 4 import java.net.URLEncoder; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.Cookie; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 public class CookieServlet extends HttpServlet { 13 14 15 public void doGet(HttpServletRequest request, HttpServletResponse response) 16 throws ServletException, IOException { 17 //该字符含有中文 18 String s="测试cookies"; 19 String encode = URLEncoder.encode(s,"UTF-8"); 20 Cookie cookie=new Cookie("Hello", encode); 21 response.addCookie(cookie); 22 response.getWriter().append("dssd"); 23 } 24 public void doPost(HttpServletRequest request, HttpServletResponse response) 25 throws ServletException, IOException { 26 } 27 }
GetCookies.java 获取cookies
1 package cn.lonecloud; 2 3 import java.io.IOException; 4 import java.net.URLDecoder; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.Cookie; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 public class GetCookies extends HttpServlet { 13 14 @Override 15 protected void doGet(HttpServletRequest req, HttpServletResponse resp) 16 throws ServletException, IOException { 17 Cookie[] cookies = req.getCookies(); 18 for (Cookie cookie : cookies) { 19 String value = cookie.getValue(); 20 String decode = URLDecoder.decode(value, "UTF-8"); 21 resp.setCharacterEncoding("UTF-8"); 22 resp.getWriter().append(decode); 23 } 24 } 25 @Override 26 protected void doPost(HttpServletRequest req, HttpServletResponse resp) 27 throws ServletException, IOException { 28 } 29 }
web.xml 配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="3.0" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 6 <servlet> 7 <servlet-name>CookieServlet</servlet-name> 8 <servlet-class>cn.lonecloud.CookieServlet</servlet-class> 9 </servlet> 10 11 <servlet-mapping> 12 <servlet-name>CookieServlet</servlet-name> 13 <url-pattern>/CookieServlet</url-pattern> 14 </servlet-mapping> 15 16 <servlet> 17 <servlet-name>getCookies</servlet-name> 18 <servlet-class>cn.lonecloud.GetCookies</servlet-class> 19 </servlet> 20 21 <servlet-mapping> 22 <servlet-name>getCookies</servlet-name> 23 <url-pattern>/getCoookies</url-pattern> 24 </servlet-mapping> 25 26 </web-app>