欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

用cookie实现查看浏览记录

程序员文章站 2022-03-09 22:41:27
...

需求:

做一个商品页面,当我们访问后,在页面上点击查看商品浏览记录后,可以查看到以前浏览过的商品信息。

步骤分析:

用cookie实现查看浏览记录
代码实现
用cookie实现查看浏览记录

<a href="ServletBook?id=1">图书一</a><br/>
<a href="ServletBook?id=2">图书二</a><br/>
<a href="ServletBook?id=3">图书三</a><br/>
<a href="ServletBook?id=4">图书四</a><br/>

封装工具类:

public class CookieUtils {
    public static Cookie findByName(String name,Cookie[] cookies){
        //1.先定义一个cookie
        Cookie cookie = null;

        //2.去数组中遍历,通过名字找指定cookie
        if (cookies!=null && cookies.length>0) {
            for (Cookie ck : cookies) {
                if (name.equals(ck.getName())) {
                    cookie = ck;
                    break;
                }
            }
        }

        return cookie;
    }
}

BookServlet

@WebServlet(name = "BookServlet", value = "/book")
public class BookServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取提交过来的id
        String id = request.getParameter("id");

        //2.获取指定的cookie(名字叫ids)
        Cookie cookie = CookieUtils.findByName("ids", request.getCookies());

        String idsValue = null;

        //3.判断有无获取到
        if (cookie == null) {
            //4.若没有获取到.暂无浏览记录,需要将本次的id记录到idscookie中
            idsValue=id;
        }else{
            //5.若获取到了 例如: ids=1-2,将cookie的值赋值给idsValue
            idsValue = cookie.getValue();

            String[] idsArr = idsValue.split("-");
            //将数组转成list,使用list的contains方法
            List<String> idsList = Arrays.asList(idsArr);
            if (!idsList.contains(id)) {
                //5.1 判断之前是否浏览过,若没有浏览过,把当前id拼接到ids中,”-id”
                idsValue = idsValue + "-" + id; //例如:1-2-4
            }
            //5.2 若浏览过,啥也不做
        }

        //6.将最新的ids的cookie写回浏览器
        cookie = new Cookie("ids",idsValue);
        cookie.setMaxAge(60*60);
        cookie.setPath(request.getContextPath());
        response.addCookie(cookie);

        //7.将图书+id写回浏览器
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.print("当前浏览的是: 图书"+id);
        writer.print("<br/>");
        //8.写回去一个超链接  点击后可以返回首页
        //writer.print("<a href='/cookie'>继续浏览</a><br/>");
        writer.print("<a href='"+request.getContextPath()+"'>继续浏览</a><br/>");
        //9.写回去一个超链接 点击后可以查询访问过那些图书
        writer.print("<a href='"+request.getContextPath()+"/history.jsp'>查看足迹</a><br/>");
    }

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

history.jsp

<%@ page import="cn.itcast.web.utils.CookieUtils" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
        //1.获取指定的cookie
        Cookie cookie = CookieUtils.findByName("ids", request.getCookies());

        //2.判断是否获取到了
        if (cookie==null) {
            //2.1如没有获取到,显示暂无浏览记录
            out.print("暂无浏览记录");
        }else{
            //2.2若有,获取cookie的value  1-2-4
            String ids = cookie.getValue();
            //切分1-2-4,形成一个数组,遍历数组获取每个id,将图书id打印到页面上
            out.print("你的足迹如下:<br>");
            for (String id : ids.split("-")) {
                out.print("图书"+id+"<br>");
            }
        }
    %>
</body>
</html>