java 使用cookie记录上次访问时间小Demo

package com.xianyu.a_cookie_rem;

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.util.Date;

/**
 * 记录上次访问时间
 */
@WebServlet(name = "RemServlet", urlPatterns = {"/rem"})
public class RemServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 0,设置编码
        response.setContentType("text/html;charset=utf-8");
        PrintWriter w = response.getWriter();

        // 1,获取指定名称的cookie
        Cookie c = getCookieByName("lastTime", request.getCookies());


        // 2判断cookie是否为空
        if (c == null) {
            //cookie为空 提示第一次访问
            w.print("您是第一次访问!");
        } else {
            //cookie不为空 获取value 展示上一次访问的时间
            String value = c.getValue();
            long time = Long.parseLong(value);
            Date date = new Date(time);
            w.print("您上次访问时间" + date.toLocaleString());
        }

        // 3,将当前访问的时间记录
        // 3.1创建cookie
        c = new Cookie("lastTime", new Date().getTime() + "");

        //持久化cookie
        c.setMaxAge(3600);
        //设置路径
        c.setPath(request.getContextPath() + "/"); //     /day11/


        // 3.2写回浏览器
        response.addCookie(c);
    }

    /**
     * 通过名称在cookie数组获取指定的cookie
     *
     * @param name    cookie名称
     * @param cookies cookies数组
     * @return
     */
    private Cookie getCookieByName(String name, Cookie[] cookies) {
        if (cookies != null) {
            for (Cookie c : cookies) {
                if (name.equals(c.getName())) {
                    return c;
                }
            }
        }
        return null;
    }
}

cookie-总结

常用方法:
        setMaxAge(int 秒):设置cookie在浏览器端存活时间  以秒为单位
            若设置成 0:删除该cookie(前提必须路径一致)
        setPath(String path):设置cookie的路径.
            当我们访问的路径中包含此cookie的path,则携带
            默认路径: 
                访问serlvet的路径,从"/项目名称"开始,到最后一个"/"结束
                例如:
                    访问的serlvet路径:
                        /day11/a/b/hello
                    默认路径为:
                        /day11/a/b
            手动设置路径:以"/项目名"开始,以"/"结尾;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您解答这个问题。 在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、付费专栏及课程。

余额充值