Cookie小案例------记住浏览过的商品记录
我们知道,这个功能在电商项目中很常见。这里处理请求和页面显示都是由servlet实现,主要是为了体现cookie的作用,
实现功能如下:
1,点击购买的商品后,显示到另一页面
2,记住用户浏览过的商品,并在页面时中显示
3,当浏览过的数量超过最大值限度时,最下面一个商品被挤下去
4,当浏览过的商品本身就在浏览记录中,显示列表将其从中间移到最上面
显示一打开网站的样子和显示用户的浏览记录:
package cn.itcast.cookie; import java.io.IOException; import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; 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 CookieDemo2 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); //1.显示网站所有商品 out.print("本网站有如下书籍:<br/>"); Map<String,Book> map = DB.getMap(); for(Map.Entry<String, Book> entry : map.entrySet()){ Book book = entry.getValue(); out.print("<a href='/day07/servlet/CookieDemo3?id="+book.getId()+"' target='_blank'>"+book.getName()+"</a><br/>"); } out.print("您曾经看过如下商品:<br/>"); //2.显示用户曾经浏览过的商品 // bookHistory Cookie cookie = null; Cookie cookies[] = request.getCookies(); for(int i=0;cookies!=null && i<cookies.length;i++){ if(cookies[i].getName().equals("bookHistory")){ cookie = cookies[i]; } } if(cookie!=null){ //找到了bookHistory这个cookie String bookHistory = cookie.getValue(); //4_6_1 String ids[] = bookHistory.split("\\_"); for(String id: ids){ Book book = (Book) DB.getMap().get(id); out.print(book.getName() + "<br/>"); } } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } class DB{ private static Map<String,Book> map = new HashMap(); static{ map.put("1", new Book("1","javaweb开发","老张")); map.put("2", new Book("2","jdbc开发","老黎")); map.put("3", new Book("3","struts2开发","老张")); map.put("4", new Book("4","spring开发","老黎")); map.put("5", new Book("5","hibernate开发","老张")); } public static Map getMap(){ return map; } } class Book{ private String id; private String name; private String author; public Book() { super(); // TODO Auto-generated constructor stub } public Book(String id, String name, String author) { super(); this.id = id; this.name = name; this.author = author; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } }
用于显示商品的详细信息:
package cn.itcast.cookie; import java.io.IOException; import java.io.PrintWriter; import java.util.Arrays; import java.util.LinkedList; 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 CookieDemo3 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); //1.根据用户带过来的id值,显示相应商品的信息 out.print("您想看的书的详细信息为:<br/>"); String id = request.getParameter("id"); Book book = (Book) DB.getMap().get(id); out.print(book.getId() + "<br/>"); out.print(book.getName() + "<br/>"); out.print(book.getAuthor() + "<br/>"); //2.以cookie的形式回写该商品的id号给浏览器 String bookHistory = makeCookie(book.getId(),request); Cookie cookie = new Cookie("bookHistory",bookHistory); cookie.setMaxAge(10000); response.addCookie(cookie); } //根据用户原来看过的书,以及现在看的书的id,构建新的cookie值 private String makeCookie(String id, HttpServletRequest request) { //bookHistory=null 3 bookHistory=3 //bookHistory=2_1_5 3 bookHistory=3_2_1 //bookHistory=2 3 bookHistory=3_2 //bookHistory=2_3 3 bookHistory=3_2 //1.得到用户曾经看过的书 String bookHistory = null; Cookie cookies[] = request.getCookies(); for(int i=0;cookies!=null && i<cookies.length;i++){ if(cookies[i].getName().equals("bookHistory")){ bookHistory = cookies[i].getValue(); } } if(bookHistory==null){ bookHistory = id; return bookHistory; } //bookHistory=1_2_5 代表用户曾经看一些书,接着程序要得到用户曾经看过什么书 String ids[] = bookHistory.split("_"); //为了检测数组中是否包含当前id,我们应该把数据转成集合,并且还要转成链表结构的集合 LinkedList<String> idList = new LinkedList(Arrays.asList(ids)); /*if(idList.contains(id)){ //bookHistory=2_3 3 bookHistory=3_2 idList.remove(id); idList.addFirst(id); }else{ //bookHistory=2_1_5 3 bookHistory=3_2_1 if(idList.size()>=3){ idList.removeLast(); idList.addFirst(id); }else{ //bookHistory=2 3 bookHistory=3_2 idList.addFirst(id); } }*/ if(idList.contains(id)){ idList.remove(id); }else{ if(idList.size()>=3){ idList.removeLast(); } } idList.addFirst(id); StringBuffer sb = new StringBuffer(); for(String lid: idList){ //1_2_3_ sb.append(lid + "_"); } return sb.deleteCharAt(sb.length()-1).toString(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
运行效果:
时间: 2024-10-09 19:04:33