利用Cookie记住上次访问时间

代码步骤,思想

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

1.有,不是第一次访问

  • 1.响应数据,欢迎回来,您上次访问时间为:2019年11月06日22点37分
  • 2.写回cookie,lasttime=2019年11月16日22点37分

2,没有,是第一次访问

  • 1.响应数据,您好,欢迎您首次访问
  • 2.写回cookie,LastTime=

注意:在tomcat 8 之后,cookie支持中文数据。但是,特殊字符还是不支持,建议使用URL编码存储,URL解码解析
编码:URLEncoder.encode(字符串, “utf-8”)
解码:URLDecoder.decode(字符串,“utf-8”)

@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.遍历cookie数组
        if (cookies != null && cookies.length>0){
            for (Cookie cookie : cookies) {
                //3.获取cookie名称
                String name = cookie.getName();
                //4.判断cookie是否为lastTime
                if("lastTime".equals(name)){
                    //有该cookie,不是第一次访问
                    flag=true;//有last TIME的cookie

                    //响应数据
                    //获取cookie的value,时间
                    String value = cookie.getValue();
                    //URL解码
                    value = URLDecoder.decode(value,"utf-8");
                    response.getWriter().write("<h1>欢迎回来,您上次访问时间为:" + value +"<h1>");


                    //设置cookie的value
                    //获取当前时间的字符串,重新设置cookie的值,重新发送cookies
                    Date date = new Date();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
                    String str_date = sdf.format(date);

                    //URL编码使cookie特殊字符支持中文
                    str_date = URLEncoder.encode(str_date, "utf-8");//是对上面的时间进行编码

                    cookie.setValue(str_date);
                    //设置cookie的存活时间
                    cookie.setMaxAge(60*60*24);//一个月
                    response.addCookie(cookie);


                   /* //响应数据
                    //获取cookie的value,时间
                    String value = cookie.getValue();

                    //URL解码
                    value = URLDecoder.decode(str_date,"utf-8");
                    response.getWriter().write("<h1>欢迎回来,您上次访问时间为:" + value +"<h1>");*/
                    break;
                }

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

            //设置cookie的value
            //获取当前时间的字符串,重新设置cookie的值,重新发送cookies
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
            String str_date = sdf.format(date);

            //URL编码使cookie特殊字符支持中文
            str_date = URLEncoder.encode(str_date, "utf-8");//是对上面的时间进行编码

            Cookie cookie = new Cookie("lastTime", str_date);
            //设置cookie的存活时间
            cookie.setMaxAge(60*60*24);//一个月
            response.addCookie(cookie);

            response.getWriter().write("<h1>您好,欢迎您首次访问<h1>");

        }
    }
好的,我可以为您解答这个问题。 在JSP页面中,可以通过Cookie对象来记录和获取上次访问时间。 下面是一个示例代码: ``` <% // 获取当前时间 Date currentDate = new Date(); // 获取上次访问时间Cookie Cookie lastVisitCookie = null; Cookie[] cookies = request.getCookies(); if (cookies != null) { for (Cookie cookie : cookies) { if (cookie.getName().equals("lastVisit")) { lastVisitCookie = cookie; break; } } } // 设置新的Cookie String lastVisitTime = "首次访问"; if (lastVisitCookie != null) { lastVisitTime = lastVisitCookie.getValue(); } Cookie newVisitCookie = new Cookie("lastVisit", currentDate.toString()); newVisitCookie.setMaxAge(60*60*24*30); // 设置Cookie的有效期为30天 response.addCookie(newVisitCookie); %> 您上次访问本站的时间是:<%= lastVisitTime %> ``` 在上面的代码中,我们首先获取了当前时间,并通过`request.getCookies()`方法获取到了上次访问时间Cookie。如果没有找到该Cookie,则说明是用户首次访问,将上次访问时间设置为“首次访问”。 接着,我们创建了一个新的Cookie对象,并通过`response.addCookie()`方法将其添加到响应中。需要注意的是,我们通过`setMaxAge()`方法设置了Cookie的有效期为30天,这样用户下次访问时也能够获取到上次访问时间。 最后,我们通过JSP的表达式语言输出了上次访问时间。 希望这个示例能够帮到您,如果您有任何疑问,请随时问我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值