您当前的位置: 首页 > 

liaowenxiong

暂无认证

  • 2浏览

    0关注

    1171博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

(案例)使用Cookie保存用户最后一次访问的时间

liaowenxiong 发布时间:2022-01-15 21:01:30 ,浏览量:2

文章目录
  • 需求
  • 分析
  • 示例代码

需求

1.访问一个Servlet,如果获取不到上一次访问的时间,则提示:您好,欢迎访问。 2.如果可以获取上一次访问的时间,则提示:您好,欢迎回来,您上次访问时间为:显示时间字符串

分析

在服务器中的Servlet判断是否有一个名为 lastTime 的 cookie 1.有

则响应数据:您好,欢迎回来,您上次访问时间为:2018年6月10日11:50: 20 写回 Cookie:lastTime=2018年6月10日11 :50:01

2.没有 响应数据:您好,欢迎访问 写回 Cookie:lastTime=2018年6月10日11 :50:01

示例代码
package priv.lwx.cs.example;
/**
 * 通过Cookie保存用户最后一次访问的时间
 *
 * @author liaowenxiong
 * @date 2022/1/14 16:39
 */


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.io.PrintWriter;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/last_time")
public class SaveLastTimeByCookieServlet extends HttpServlet {
  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    this.doPost(request, response);
  }

  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html;charset=utf-8");
    boolean flag = false;
    PrintWriter writer = response.getWriter();
    // 获取所有的Cookie
    Cookie[] cookies = request.getCookies();
    // 遍历Cookie数组,查找是否存在名为lasttime的Cookie
    if (cookies != null && cookies.length != 0) {
      for (Cookie cookie : cookies) {
        String name = cookie.getName();
        if ("lastTime".equals(name)) {
          // 存在名为lastTime的Cookie,则将变量flag的值设为true
          flag = true;
          String value = cookie.getValue();
          // value是application/x-www-form-urlencoded字符串,需要解码
          String decDate = URLDecoder.decode(value, "utf-8");
          writer.write("您好,欢迎回来,您上次访问的时间:" + decDate + "");
          // 获取系统当前时间
          Date date = new Date();
          // 创建日期格式对象
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
          // 格式化日期对象,返回一个日期字符串
          String strDate = sdf.format(date);
          System.out.println("URL编码前:" + strDate);
          // URL编码,因为Cookie无法存储空格等特殊字符,所以只能将含有空格的字符串转换成application/x-www-form-urlencoded字符串进行存储
          String encDate = URLEncoder.encode(strDate, "utf-8");
          System.out.println("URL编码后:" + encDate);

          // 用最新的系统时间替换名为lastTime的Cookie中旧的日期值
          cookie.setValue(encDate);
          // 将名为lastTime的Cookie添加到Response对象中
          response.addCookie(cookie);
          break;
        }
      }
    }
    // 没有名为lastTime的Cookie
    if (cookies == null || cookies.length == 0 || flag == false) {
      // 获取系统当前时间
      Date date = new Date();
      // 创建日期格式对象
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
      // 格式化日期对象,返回一个日期字符串
      String strDate = sdf.format(date);
      System.out.println("URL编码前:" + strDate);
      // URL编码,因为Cookie无法存储空格等特殊字符,所以只能将含有空格的字符串转换成application/x-www-form-urlencoded字符串进行存储
      String encDate = URLEncoder.encode(strDate, "utf-8");
      System.out.println("URL编码后:" + encDate);
      // 创建Cookie
      Cookie cookie = new Cookie("lastTime", encDate);
      // 设置Cookie的存活时间为1个月
      cookie.setMaxAge(30*24*60*60);
      response.addCookie(cookie);
      writer.write("您好,欢迎访问!");
    }
  }
}

注意:Cookie无法保存空格等字符串,需要进行URL编码进行保存,服务端取值时要URL解码

关注
打赏
1661566967
查看更多评论
立即登录/注册

微信扫码登录

0.0421s