创建时间:6.30 java.lang.IllegalArgumentException: An invalid character [32] was present in the Cookie value
报错原因:
Tomcat 8.5版本,在cookie值中不能使用空格。
代码:
1 protected void doGet(HttpServletRequest request, HttpServletResponse response) 2 throws ServletException, IOException { 3 4 Date date = new Date(); 5 SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 6 String currentTime = format.format(date); 7 8 Cookie accessTime = new Cookie("lastAccessTime",currentTime); 9 accessTime.setMaxAge(60*50); 10 response.addCookie(accessTime); 11 12 String lastAccessTime=null; 13 Cookie[] cookies = request.getCookies(); 14 if (cookies != null) { 15 for(Cookie cookie:cookies) { 16 if(cookie.getName().equals("laseAccessTime")) { 17 lastAccessTime=cookie.getValue(); 18 } 19 } 20 } 21 22 if(lastAccessTime==null) { 23 response.setContentType("text/html;charset=UTF-8"); 24 response.getWriter().write("您是第一次访问"); 25 System.out.println(lastAccessTime); 26 } 27 else { 28 response.getWriter().write("您上次访问的时间是:"+lastAccessTime); 29 } 30 31 }
问题出在这里:日期格式有空格,换成/即可
原文地址:https://www.cnblogs.com/musecho/p/11237165.html
时间: 2024-10-24 11:50:18