zoukankan      html  css  js  c++  java
  • 【Cookie】java.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     }

     

    问题出在这里:日期格式有空格,换成/即可

  • 相关阅读:
    Cryptography I 学习笔记 --- 使用分组密码
    Cryptography I 学习笔记 --- 分组密码
    jQuery动画之自定义动画
    jQuery事件之一次性事件
    jQuery事件之自定义事件
    jQuery事件之解绑事件
    jQuery事件之绑定事件
    jQuery动画之停止动画
    JQuery动画之淡入淡出动画
    jQuery属性操作之值操作
  • 原文地址:https://www.cnblogs.com/musecho/p/11237165.html
Copyright © 2011-2022 走看看