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,

An invalid character [32] was present in the Cookie value 错误

后来发现32对应的编码是空格,后来发现

  1. SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");代码中产生了空格,后改为
  1. SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd-hh:mm:ss");就可正常访问了
    An invalid character [32] was present in the Cookie value 错误

 

 

相关文章:

  • 2022-12-23
  • 2021-11-16
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-13
  • 2021-04-25
  • 2022-02-27
猜你喜欢
  • 2022-12-23
  • 2021-09-06
  • 2022-12-23
  • 2022-01-11
  • 2021-11-28
相关资源
相似解决方案