An invalid character [32] was present in the Cookie value 错误
An invalid character [32] was present in the Cookie value 错误
LexBBQ 2018-04-09 原文
今天在做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 Cookie cookie =new Cookie("Lastname",Last);
7 cookie.setMaxAge(60*10*500);
8 response.addCookie(cookie);
9 //获得用户携带的cookie
10 String last=null;
11 Cookie[] cookies = request.getCookies();
12 if(cookies!=null){
13 for(Cookie coo:cookies){
14 if("Lastname".equals(coo.getName())){
15 last = coo.getValue();
16
17 }
18 }
19 }
20
21 response.setContentType("text/html;charset=utf-8");
22 if(last==null){
23
24 response.getWriter().write("您是第一次访问");
25 }else{
26 response.getWriter().write("你上次访问的时间为"+last);
27 }
再访问该Servlet的时候页面就为500,并报异常An invalid character [32] was present in the Cookie value,
后来发现32对应的编码是空格,后来发现
SimpleDateFormat format=newSimpleDateFormat(“yyyy-MM-dd hh
ss”);代码中产生了空格,后改为
SimpleDateFormat format=newSimpleDateFormat(“yyyy-MM-dd-hh
ss”);就可正常访问了
转载于//www.cnblogs.com/studyMoreRich/articles/exception-AnInvalidCharacter.html
还没有评论,来说两句吧...