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

 package cn.lonecloud;

 import java.io.IOException;
import java.net.URLEncoder; import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class CookieServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//该字符含有中文
String s="测试cookies";
String encode = URLEncoder.encode(s,"UTF-8");
Cookie cookie=new Cookie("Hello", encode);
response.addCookie(cookie);
response.getWriter().append("dssd");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
GetCookies.java  获取cookies
 package cn.lonecloud;

 import java.io.IOException;
import java.net.URLDecoder; import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class GetCookies extends HttpServlet { @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Cookie[] cookies = req.getCookies();
for (Cookie cookie : cookies) {
String value = cookie.getValue();
String decode = URLDecoder.decode(value, "UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.getWriter().append(decode);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
}
}

web.xml  配置文件

 <?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>CookieServlet</servlet-name>
<servlet-class>cn.lonecloud.CookieServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>CookieServlet</servlet-name>
<url-pattern>/CookieServlet</url-pattern>
</servlet-mapping> <servlet>
<servlet-name>getCookies</servlet-name>
<servlet-class>cn.lonecloud.GetCookies</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>getCookies</servlet-name>
<url-pattern>/getCoookies</url-pattern>
</servlet-mapping> </web-app>
 
上一篇:Cookie不能保存中文解决方式


下一篇:杂-lowbit