一个关于cookie的坑

#问题:今天上午踩了一个坑,首先,这个小案例的运行结果是应该是在前端页面出现一个当前时间的,当然如果是首次登陆的话应该是显示"第一次登陆",第二次则会显示上次的登录时间,但是却没有显示,首先看tomcat,会报一个异常IllegeArgumentException, 即非法参数异常, 

#总结:1)tomcat8.5及以上的版本不允许有空格,而空格在对应的ASCII是32;

                2)习惯看tomcat日志以及debug ;

#以下是具体的DEMO:

Servlet文件:

package cn.itcast.chapter05.cookie.example;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 需求:当用户访问某些Web应用时,经常会显示该用户上一次的访问时间。
 * 实现:用Cookie技术实现显示用户上次的访问时间的功能。
 * 本类:用于实现获取Cookie信息并将当前时间作为Cookie的值发送给客户端。
 * */
public class LastAccessServlet extends HttpServlet {
    private static final long serialVersionUID=1L;
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");

        String lastAccessTime=null;

        //获取所有的cookie,并将这些cookie存放当数组中
        Cookie[] cookies=request.getCookies();

        //遍历cookies数组
        for (int i = 0; cookies!=null&&i<cookies.length; i++) {
            if ("lastAccess".equals(cookies[i].getName())){
                lastAccessTime=cookies[i].getValue();
                break;
            }
        }

        //判断是否存在名称为lastAccess的cookie
        if(lastAccessTime==null){
            response.getWriter().print("你是首次访问本站!");
        }else {
            response.getWriter().print("你上次访问的时间是:"+lastAccessTime);
        }
        //创建cookie,将当前时间作为cookie的值发送给客户端
        String currentTime=
                new SimpleDateFormat("yyyy-MM-dd-hh:mm:ss").format(new Date());
        Cookie cookie=new Cookie("lastAccess",currentTime);
        cookie.setMaxAge(60*60);
        response.addCookie(cookie);
    }

}

web.xml文件,配置servlet路径。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">

  <servlet>
    <servlet-name>LastAccess</servlet-name>
    <servlet-class>cn.itcast.chapter05.cookie.example.LastAccessServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>LastAccess</servlet-name>
    <url-pattern>/LastAccess</url-pattern>
  </servlet-mapping>

</web-app>

#Cookie回忆

1)概念:Cookie是保存在浏览器端的数据。

             当浏览器访问服务器,服务器会发送一个set-cookie消息头给浏览器,当再次访问服务器时,会将

             该消息头发送给服务器。

2)使用:

           1.添加Cookie
             Cookie c = new Cookie(String name,String value);
             注:
             Cookie只能存放字符串。
             response.addCookie(c);
           2.读取Cookie
             Cookie[] request.getCookies();
             String cookie.getName();
             String cookie.getValue();
             注: 有可能返回null。
             一个Cookie对象封装了一个Cookie中的数据。
 
#说明

           1.学会重复,与其敲三万行代码,不如把一万行代码敲三遍。

           2.学会在集体中成长,学会借助外力。

成年人的世界没有那么多的童话,也没有那么多的逆袭。
原文地址:https://www.cnblogs.com/shijinglu2018/p/10627505.html