今天在做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對應的編碼是空格,后來發現

  1. SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");代碼中產生了空格,后改為
  1. SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd-hh:mm:ss");就可正常訪問了