使用cookie技术实现大型网站购物网站的商品浏览记录
下面我们用cookie技术来做一个商品浏览记录的案例,
第一步:先建一个jsp的页面,用来模拟数据库,存储商品的列表
代码如下:
jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page import="java.util.*"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h1>商品列表</h1> <ul> <li><a href="/myday07/addcookie?id=1">java基础</a></li> <li><a href="/myday07/addcookie?id=2">javaweb</a></li> <li><a href="/myday07/addcookie?id=3">DHTML</a></li> <li><a href="/myday07/addcookie?id=4">数据库</a></li> <li><a href="/myday07/addcookie?id=5">Struts</a></li> <li><a href="/myday07/addcookie?id=6">Spring</a></li> <li><a href="/myday07/addcookie?id=7">Hibernate</a></li> </ul> <h1>最近访问记录</h1><h5><a href="/myday07/clearcookie">清空数据</a> </h5> <% String[] arr = {"java基础","javaweb","DHTML","数据库","Struts","Spring","Hibernate"}; // 获得所有cookie Cookie[] cookies = request.getCookies(); // 在cookie中查找商品浏览记录 String visitlist = null; if(cookies!=null){ for (Cookie cookie : cookies) { if (cookie.getName().equals("visitlist")) { visitlist = cookie.getValue(); break; } } if(visitlist==null){ // 商品列表不存在 out.println("无"); }else{ String[] existIds = visitlist.split(","); for(String existId:existIds){ int id = Integer.parseInt(existId); out.println(arr[id-1]+"<br/>"); } } } %> <hr/> </body> </html>第二步:使用addCookie页面来处理商品的浏览记录请求,并记下商品的访问记录,代码如下:package cn.itcast.cookie; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class addCookie extends HttpServlet { // 获得用户当前访问商品,更新到商品浏览记录列表 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 自动刷新 response.setHeader("refresh", "2;url=/myday07/products1.jsp"); // 1、获得当前用户浏览商品id String id = request.getParameter("id"); // 2、获得用户浏览记录列表 Cookie[] cookies = request.getCookies(); // 查找访问记录列表 name:visitlist ----- 1,3,5,6 String visitlist = null; if (cookies != null) { // 存在cookie for (Cookie cookie : cookies) { if (cookie.getName().equals("visitlist")) { visitlist = cookie.getValue(); break; } } if (visitlist == null) { // 列表不存在,当前商品第一件商品 Cookie cookie = new Cookie("visitlist", id); cookie.setMaxAge(60 * 60); response.addCookie(cookie); response.getWriter().println("add success"); } else { // 3、判断当前商品是否已经在列表中 String[] existIds = visitlist.split(","); for (String exsitId : existIds) { if (exsitId.equals(id)) { // id存在在列表中 response.getWriter().println("add success"); return; } } // 4、如果当前商品不在列表中,添加到列表 Cookie cookie = new Cookie("visitlist", visitlist + "," + id); cookie.setMaxAge(60 * 60); response.addCookie(cookie); response.getWriter().println("add success"); } } else { // 表示 并没有该站点的任何cookie Cookie cookie = new Cookie("visitlist", id); cookie.setMaxAge(60 * 60);//设置cookie的有效期为1个小时 response.addCookie(cookie); response.getWriter().println("add success"); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response);} }第三步:写一个清除访问记录的界面,clear页面,clearCookie页面,代码如下:这个的原理就是设置cookie的有效期,让他的有效期设置为0,这样cookie就会被清除package cn.itcast.cookie; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class clearCookie extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Cookie cookie=new Cookie("visitlist",""); cookie.setMaxAge(0); response.addCookie(cookie); response.sendRedirect("/myday07/products1.jsp"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
如果,这篇博客帮您解决了问题,不妨点击一下右下角的【推荐】。如果,您希望更容易地发现我的新博客,不妨点击一下【加关注】。因为,我的热情需要您的肯定和支持!感谢您的阅读,如果文章中有错误或者您有什么好的建议,也欢迎您直接留言批评指教。Thanks,friends! |