Cookiejava.lang.IllegalArgumentException An invalid character [32] was present in the Cookie value
Posted musecho
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Cookiejava.lang.IllegalArgumentException An invalid character [32] was present in the Cookie value相关的知识,希望对你有一定的参考价值。
创建时间: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
问题出在这里:日期格式有空格,换成/即可
以上是关于Cookiejava.lang.IllegalArgumentException An invalid character [32] was present in the Cookie value的主要内容,如果未能解决你的问题,请参考以下文章