Cookie 案例记住上一次访问时间
发布日期:2021-05-07 19:41:36 浏览次数:18 分类:精选文章

本文共 3923 字,大约阅读时间需要 13 分钟。

文章目录

一、需求分析

  1. 需求:
(1)访问一个Servlet,如果是第一次访问,则提示:您好,欢迎您首次访问。(2)如果不是第一次访问,则提示:欢迎回来,您上次访问时间为:显示时间字符串
  1. 分析:
(1)可以采用Cookie来完成(2)在服务器中的 Servlet 判断是否有一个名为 lastTime 的 cookie			* 有:不是第一次访问				* 响应数据:欢迎回来,您上次访问时间为:xx年月x日xxxx,所以需要先打印value值,再更新				* 写回Cookie:lastTime=xx年x月xx日x:x:x			* 没有:是第一次访问				* 响应数据:您好,欢迎您首次访问				* 写回Cookie:lastTime=x年x月x日x:x:x

二、代码实现

代码编写好后,发现:ASCII 码32也就是空格出现在 设置的 cookie value 中

在这里插入图片描述
在这里插入图片描述
虽然我们已经设置了响应消息的数据格式及编码,但是这里对于一些特殊的字符,仍是不能很好地识别,因此需要对其进行编码解码操作

如果使用 JSP,通过 EL 表达式可以解决

//                    为了处理cookie 中 value 的 特殊字符,需要对其进行编码//                    URL 编码                    String encode = URLEncoder.encode(format, "utf-8");//                    将编码后的值设置进去                    cookie.setValue(encode);                    response.addCookie(cookie);//                    响应数据,获取 Cookie 的 value,时间                    String value = cookie.getValue();//                    由于对 value 设置了编码,所以这里需要对其进行解码,才能正常显示                    String decode = URLDecoder.decode(value, "utf-8");                    response.getWriter().write("

欢迎回来,您上次访问的时间为:" + decode +"

"); break;

代码:

@WebServlet("/cookieTest")public class CookieTest extends HttpServlet {       protected void doPost(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)遍历 cookies 数组        if (cookies != null && cookies.length > 0) {               for (Cookie cookie : cookies) {   //                (3)获取 cookie 的名称                String name = cookie.getName();//                (4)判断名称是否是 lastTime                if ("lastTime".equals(name)){   //                    有该 cookie,不是第一次访问                    flag = true;//                    响应数据,获取 Cookie 的 value,时间                    String value = cookie.getValue();//                    由于对 value 设置了编码,所以这里需要对其进行解码,才能正常显示                    String decode = URLDecoder.decode(value, "utf-8");                    response.getWriter().write("

欢迎回来,您上次访问的时间为:" + decode +"

");// 获取之后,将 value 更新为新的值// 获取当前时间的字符串,重新设置 Cookie 的值,重新发送 cookie Date date = new Date(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); String format = simpleDateFormat.format(date);// 为了处理cookie 中 value 的 特殊字符,需要对其进行编码 System.out.println("编码前:" + format);// URL 编码 String encode = URLEncoder.encode(format, "utf-8"); System.out.println("编码后:" + encode);// 将编码后的值设置进去 cookie.setValue(encode);// 设置 cookie 的存活时间为一个月 cookie.setMaxAge(60*60*24*30); response.addCookie(cookie); break; } } } if (cookies == null || cookies.length == 0 || flag == false) { // 属于第一次访问的情况// 设置 Cookie 的 value// 获取当前时间的字符串,重新设置 Cookie 的值,重新发送 cookie Date date = new Date(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); String format = simpleDateFormat.format(date);// 为了显示特殊字符需要对其进行编码处理 String encode = URLEncoder.encode(format, "utf-8"); Cookie cookie = new Cookie("lastTime", encode);// 设置 cookie 的存活时间 cookie.setMaxAge(60*60*24*30); response.addCookie(cookie); response.getWriter().write("

您好,欢迎您首次访问!

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

在这里插入图片描述

上一篇:JSP 入门学习
下一篇:cookie 的实现细节及特点

发表评论

最新留言

关注你微信了!
[***.104.42.241]2025年04月15日 20时20分58秒