HTTP Status 500 – Internal Server Error
Type Exception Report
Message An invalid character [32] was present in the Cookie value
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
java.lang.IllegalArgumentException: An invalid character [32] was present in the Cookie value org.apache.tomcat.util.http.Rfc6265CookieProcessor.validateCookieValue(Rfc6265CookieProcessor.java:182) org.apache.tomcat.util.http.Rfc6265CookieProcessor.generateHeader(Rfc6265CookieProcessor.java:115) org.apache.catalina.connector.Response.generateCookieString(Response.java:1019) org.apache.catalina.connector.Response.addCookie(Response.java:967) org.apache.catalina.connector.ResponseFacade.addCookie(ResponseFacade.java:386) com.itheima.lastaccesstime.LastAccessTimeServlet.doGet(LastAccessTimeServlet.java:43) javax.servlet.http.HttpServlet.service(HttpServlet.java:635) javax.servlet.http.HttpServlet.service(HttpServlet.java:742) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Note The full stack trace of the root cause is available in the server logs.
Apache Tomcat/8.5.16
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获得当前时间 Date date = new Date(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-ddhh:mm:ss");// 中间不能有空格 String currentTime = format.format(date); // 1.创建Cookie记录当前的最新访问时间 Cookie cookie = new Cookie("lastAccessTime", currentTime); cookie.setMaxAge(60 * 10 * 500); response.addCookie(cookie); // 2.获得客户端携带的cookie--lastAccessTime String lastAccessTime = null; Cookie[] cookies = request.getCookies(); if (cookies != null) { for (Cookie coo : cookies) { if ("lastAccessTime".equals(coo.getName())) { lastAccessTime = cookie.getValue(); } } } response.setContentType("text/html;charset=UTF-8"); if (lastAccessTime == null) { response.getWriter().write("你是第一次访问"); } else { response.getWriter().write("你上次的访问访问是:" + lastAccessTime); } }
解决办法:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-ddhh:mm:ss");// 中间不能有空格
时间: 2024-12-09 15:23:12