tomcat 9.0中,用cookie进行会话时出现的不合法字符问题

An invalid character [32] was present in the Cookie value
An invalid character [59] was present in the Cookie value
在tomcat9.0中,使用cookie会话时出现了上述的字符不合法的异常,查看api文档中cookie对象的描述发现,只是对cookie的name的命名进行了规范,即要求按照Java的变量命名规范(参数name需要遵循规范RFC2109,也就是说name参数只能包含字母或者数字,不能包含逗号、分号、空格及美元符号),对于value值无任何约束,但实际并不是如此,这可能是使用utf-8编码独有的问题,以下为出现异常的原因:在cookie值中不能使用分号(;)、逗号(,)、等号(=)以及空格,否则会出现异常。以下为例子
//获取当前时间
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String curtime = format.format(new Date());

// 创建一个cookie对象
Cookie c = new Cookie("lasttime", curtime);
严重: Servlet.service() for servlet [com.kooking.cookie.CookieDemo] in context with path [/Servlet_Test1] threw exception
java.lang.IllegalArgumentException: An invalid character [59] was present in the Cookie value
    at org.apache.tomcat.util.http.Rfc6265CookieProcessor.validateCookieValue(Rfc6265CookieProcessor.java:182)
    at org.apache.tomcat.util.http.Rfc6265CookieProcessor.generateHeader(Rfc6265CookieProcessor.java:115)
    at org.apache.catalina.connector.Response.generateCookieString(Response.java:1010)
    at org.apache.catalina.connector.Response.addCookie(Response.java:962)
    at org.apache.catalina.connector.ResponseFacade.addCookie(ResponseFacade.java:385)
    at com.kooking.cookie.CookieDemo.doGet(CookieDemo.java:79)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:475)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:498)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:796)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1374)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

解决方法:不使用上述的特殊字符,若在js中可以使用escape()函数进行编码在取出值以后需要使用unescape()进行解码

你可能感兴趣的:(servlet)