错误信息:
HTTP Status 500 - An invalid character [32] was present in the Cookie value
type Exception report
message An invalid character [32] was present in the Cookie value
description The server encountered an internal error that prevented it from fulfilling this request.
exception
java.lang.IllegalArgumentException: An invalid character [32] was present in the Cookie value
org.apache.tomcat.util.http.Rfc6265CookieProcessor.validateCookieValue(Rfc6265CookieProcessor.java:162)
org.apache.tomcat.util.http.Rfc6265CookieProcessor.generateHeader(Rfc6265CookieProcessor.java:111)
org.apache.catalina.connector.Response.generateCookieString(Response.java:989)
org.apache.catalina.connector.Response.addCookie(Response.java:937)
org.apache.catalina.connector.ResponseFacade.addCookie(ResponseFacade.java:386)
CookieDemo.doGet(CookieDemo.java:26)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/8.5.11 logs.
Apache Tomcat/8.5.11
部分Java代码为:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
Cookie lastCookie = new Cookie("lastCookie",df.format(new Date()));
lastCookie.setMaxAge(3000);
resp.addCookie(lastCookie);
解决方法:
将SimpleDateFormat df = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);代码中的空格替换为其他连接符(dd跟hh中间有空格)