An invalid character [32] was present in the Cookie value 错误
今天在做cookie部分的demo的时候出现了一个错误Servlet部分的代码如下
1 Date data=new Date(); 2 SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 3 String Last = format.format(data); 4 // System.out.println(Last); 5 6 7 Cookie cookie =new Cookie("Lastname",Last); 8 cookie.setMaxAge(60*10*500); 9 response.addCookie(cookie); 10 //获得用户携带的cookie 11 String last=null; 12 Cookie[] cookies = request.getCookies(); 13 if(cookies!=null){ 14 for(Cookie coo:cookies){ 15 if("Lastname".equals(coo.getName())){ 16 last = coo.getValue(); 17 18 } 19 } 20 } 21 22 response.setContentType("text/html;charset=utf-8"); 23 if(last==null){ 24 25 response.getWriter().write("您是第一次访问"); 26 }else{ 27 response.getWriter().write("你上次访问的时间为"+last); 28 }
再访问该Servlet的时候页面就为500,并报异常An invalid character [32] was present in the Cookie value,

后来发现32对应的编码是空格,后来发现
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");代码中产生了空格,后改为
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd-hh:mm:ss");就可正常访问了
![]()

浙公网安备 33010602011771号