Cookie小案例:记住上次的访问时间

需求

1. 访问一个Servlet,如果是第一次访问,则提示:您好,欢迎您首次访问。
2. 如果不是第一次访问,则提示:欢迎回来,您上次访问时间为:显示时间字符串

代码实现

package com.example.day16.cookie;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import javax.xml.crypto.Data;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 在服务器中的Servlet判断是否有一个名为lastTime的cookie
 1. 有:不是第一次访问
 1. 响应数据:欢迎回来,您上次访问时间为:2018年6月10日11:50:20
 2. 写回Cookie:lastTime=2018年6月10日11:50:01
 2. 没有:是第一次访问
 1. 响应数据:您好,欢迎您首次访问
 2. 写回Cookie:lastTime=2018年6月10日11:50:01

 */
@WebServlet("/CookieTest")
public class CookieTest extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置响应的消息体的数据格式以及编码
        response.setContentType("text/html;charset=utf-8");

        //1.获取所有Cookie
        Cookie[] cookies = request.getCookies();
        boolean flag = false;//没有Cookie为lastTime
        //2.遍历cookie数组
        if(cookies !=null && cookies.length >0){
            for (Cookie cookie : cookies) {
                //获取cookie名称
                String name = cookie.getName();
                //4.判断名称是否是:LastTime
                if("LastTime".equals(name)){
                    //有该Cookie,不是第一次访问
                    flag = true;    //有lastTime的cookie

                    //设置cookie的value
                    //获取当前时间的字符串,重新设置Cookie的值,重新发送Cookie
                    Date date = new Date();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
                    String str_date = sdf.format(date);
                    System.out.println("编码前:"+str_date);
                    //进行URL编码(主要解决刚刚格式化中的空格,如果不进行编码,运行时,会爆出500错误)
                    str_date = URLEncoder.encode(str_date,"utf-8");
                    System.out.println("编码后:"+str_date);
                    //覆盖以往的cookie值
                    cookie.setValue(str_date);
                    //设置cookie的存活时间
                    cookie.setMaxAge(60*60*24*30);//设置存活时间为一个月
                    //发送cookie
                    response.addCookie(cookie);

                    //响应数据
                    //获取Cookie的value,时间
                    String value = cookie.getValue();
                    System.out.println("解码前"+value);
                    //URL解码:因为前面进行编码,所以此处要进行解码,否则会出错并且不能阅读
                    value = URLDecoder.decode(value, "utf-8");
                    System.out.println("解码后"+value);
                    //输出欢迎语
                    response.getWriter().write("<h1>欢迎回来,您上次的登录时间为:"+value+"</h1>");
                    break;
                }
            }
        }

        if(cookies == null || cookies.length ==0 || flag == false){
            //第一次访问

            //设置cookie的value
            //获取当前时间的字符串,重新设置Cookie的值,重新发送Cookie
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
            String str_date = sdf.format(date);
            System.out.println("编码前:"+str_date);
            //进行URL编码(主要解决刚刚格式化中的空格,如果不进行编码,运行时,会爆出500错误)
            str_date = URLEncoder.encode(str_date,"utf-8");
            System.out.println("编码后:"+str_date);
            //覆盖以往的cookie值
            Cookie cookie = new Cookie("LastTime",str_date);
            cookie.setValue(str_date);
            //设置cookie的存活时间
            cookie.setMaxAge(60*60*24*30);//设置存活时间为一个月
            //发送cookie
            response.addCookie(cookie);


            //响应数据
            //获取Cookie的value,时间
            String value = cookie.getValue();
            System.out.println("解码前"+value);
            //URL解码:因为前面进行编码,所以此处要进行解码,否则会出错并且不能阅读
            value = URLDecoder.decode(value, "utf-8");
            System.out.println("解码后"+value);
            //输出欢迎语
            response.getWriter().write("<h1>你好,欢迎首次登录</h1>");



        }
    }

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

运行结果

在这里插入图片描述
在这里插入图片描述

菜鸟想说:如果不是太着急的话,就对照着代码敲一遍理解一下,直接复制的话,可能会不懂,如果您是编程大神的话,请忽略。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值