Case (web development): a record time access (use Cookie knowledge)

Case - time record first visit (focus)

Requirements: If the user is the first visit CookieServlet, print the browser is your first visit if the user is not the first visit, give the browser print your last visit was on xxxx..

Analysis: The
Here Insert Picture Description
code to achieve:

package com.ccc.cookie_project;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
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;

/**
 1.获取所有的cookie  (如果cookie的是空或者,name不是我们想要的)说明是第一次访问.
 2.如果是第一次访问
     2.1创建cookie ("time",当前时间字符串)
     2.3将创建的cookie放到response中
     2.3给浏览器打印一句话 您是第一次访问
 3.如果不是第一次访问 (已经获取到了名称为time的cookie)
     3.1获取cookie中的内容(上一次访问的时间)
     3.2cookie重新设置当前时间
     3.3给response中设置cookie
     3.4给浏览器打印一句话, 您上一次访问的时间是xxx
 */
@WebServlet("/CookieServlet")
public class CookieServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置乱码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");

        // 1.获取所有的cookie  (如果cookie的是空或者,name不是我们想要的)说明是第一次访问.
        Cookie[] cookies = request.getCookies();  
        
        //获取到了所有的cookie,查询看里面我们想要的cookie,即key值是time
        
        //1.1编写一个方法从所有的cookie查找key值为time的cookie
        //cookie如果是null 没查到第一次访问 如果不是null不是第一次访问
        Cookie cookie = findCookieByName(cookies,"time");
        //2.如果是第一次访问
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd/HH:mm:ss");
        String time = sdf.format(date);
        if(cookie == null){
            //2.1创建cookie ("time",当前时间字符串)

            Cookie c = new Cookie("time",time);
            //补充:
            c.setMaxAge(60*60*24*7);
            c.setPath("/day14");
            //2.3将创建的cookie放到response中
            response.addCookie(c);
           //2.3给浏览器打印一句话 您是第一次访问
            response.getWriter().print("<h2>您是第一次访问</h2>");

        }else{
			// 3.如果不是第一次访问 (已经获取到了名称为time的cookie)
            
            //3.1获取cookie中的内容(上一次访问的时间)
            String value = cookie.getValue();  //value是上一次cookie中记录的时间
            //3.2cookie重新设置当前时间
            cookie.setValue(time);
            //3.3给response中设置cookie
            response.addCookie(cookie);
            //3.4给浏览器打印一句话, 您上一次访问的时间是xxx
            response.getWriter().print("<h2>您上一次访问的事件是"+value+"</h2>");
        }
    }

    //根据指定的名称查找cookie信息
    private Cookie findCookieByName(Cookie[] cookies, String time) {
        //1.判断cookies数组是否为null,判断cookies数组的长度是否大于0,如果满足这两个条件循环.
        //不然就返回null
        if(cookies != null && cookies.length>0){
            for (Cookie cookie : cookies) {
                //循环中判断,如果cookie的名字和传递参数name值一致,直接返回该cookie
                if(time.equals(cookie.getName())){
                    return cookie;
                    //return cookie;这句代码的大括号{}可以省略,因为if条件成立之后,
                    //默认执行离他最近的一句话
                }
            }
            
        }else{
            return null;
        }
        //如果有cookie,但是没有我们需要的key为time的cookie,仍然返回null
        return null;
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}

Guess you like

Origin blog.csdn.net/qq_45083975/article/details/92965972