java + tomcat cookie 异常

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java + tomcat cookie 异常相关的知识,希望对你有一定的参考价值。


 
        Cookie cookie = new Cookie(username,value);
                cookie.setMaxAge(60*60*24*7,cookie);
                response.addCookie(cookie);

在执行到 response.addCookie(cookie); 时 报错,发现在 cookie中保存的 username 变量是 英文时无问题,如果是中文则会出现以下异常~

严重: Servlet.service() for servlet [Login] in context with path [/learntest] threw exception
java.lang.IllegalArgumentException: Control character in cookie value or attribute.
    at org.apache.tomcat.util.http.LegacyCookieProcessor.needsQuotes(LegacyCookieProcessor.java:431)
    at org.apache.tomcat.util.http.LegacyCookieProcessor.generateHeader(LegacyCookieProcessor.java:303)
    at org.apache.catalina.connector.Response.generateCookieString(Response.java:959)
    at org.apache.catalina.connector.Response.addCookie(Response.java:907)
    at org.apache.catalina.connector.ResponseFacade.addCookie(ResponseFacade.java:392)
    at com.test.cookietest.Login.doPost(Login.java:74)
    at com.test.cookietest.Login.doGet(Login.java:41)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2500)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2489)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

 

原因(猜测):

  由于cookie 中只能以ASCII码的格式进行保存(即只能保存英文)  而在 Cookie.addCookie 方法中检查了 保存的文本内容 发现无法识别 抛出异常

解决方案:

  在把变量保存到 cookie对象中之前 先 对 username 变量 使用 URLEncoder工具类 进行编码,使之以 unicode 编码格式进行保存

  在提取 cookie 中 username 变量时 先使用 URLDecoder 工具类进行解码,使之以 utf-8 进行使用。

  value = URLEncoder.encode(value, "utf-8");
        Cookie cookie = new Cookie(username,value);

  cookie.setMaxAge(60*60*24*7,cookie);

   response.addCookie(cookie);

 问题解决

 





































以上是关于java + tomcat cookie 异常的主要内容,如果未能解决你的问题,请参考以下文章

异常:java.lang.IllegalArgumentException: Control character in cookie value or attribute.

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

细节问题与知识

使用Cookie报错“An invalid character [32] was present in the Cookie value“

idea tomcat热部署 Error running 'Tomcat 7': Unable to open debugger port (127.0.0.1:3622): java

Tomcat异常:server Tomcat v9.09 Server at localhost failed to start