【JSP开发】利用cookie实现商品浏览记录

在主页中显示商品,点击商品可以查看商品的具体信息,带用户浏览完之后,回到主页,就会看到自己曾经浏览过什么商品。


商品主页Servlet
  1. CookieDemo3.java:  
  2. package cn.edu.cookie;  
  3.   
  4.   
  5. import java.io.IOException;  
  6. import java.io.PrintWriter;  
  7. import java.util.LinkedHashMap;  
  8. import java.util.Map;  
  9.   
  10.   
  11. import javax.servlet.ServletException;  
  12. import javax.servlet.http.Cookie;  
  13. import javax.servlet.http.HttpServlet;  
  14. import javax.servlet.http.HttpServletRequest;  
  15. import javax.servlet.http.HttpServletResponse;  
  16. //代表首页的Servlet  
  17. public class CookieDemo3 extends HttpServlet {  
  18.   
  19.   
  20.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  21.             throws ServletException, IOException {  
  22.           
  23.         response.setCharacterEncoding("UTF-8");  
  24.         response.setContentType("text/html;charset=UTF-8");  
  25.         PrintWriter out=response.getWriter();  
  26.           
  27.        //1.输出网站所有商品  
  28.         out.write("本网站有如下商品:<br/>");  
  29.         Map<String,Book> map = Db.getAll();  
  30.         for(Map.Entry<String, Book> entry :map.entrySet())  
  31.         {  
  32.             Book book=entry.getValue();  
  33.             out.print("<a href='/day07/CookieDemo4?id="+book.getId()+"' terget='_blank'>"+book.getName()+"</a><br/>");  
  34.         }  
  35.           
  36.        //2.输出用户曾经看过的商品  
  37.         out.print("您曾经看过如下商品:<br/>");  
  38.         Cookie cookies[]= request.getCookies();  
  39.         for(int i=0;cookies!=null&&i<cookies.length;i++)  
  40.         {  
  41.             //是不是要加if(cookies[i].getName().equals("BookHistory"))判断?  
  42.             String ids[]=cookies[i].getValue().split("\\,");  
  43.             for(String id:ids){  
  44.                 Book book=(Book)Db.getAll().get(id);  
  45.                 out.print(book.getName()+"<br/>");  
  46.             }  
  47.               
  48.         }  
  49.     }  
  50.   
  51.   
  52.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  53.             throws ServletException, IOException {  
  54.        doGet(request,response);  
  55.     }  
  56.   
  57.   
  58. }  
  59.   
  60.   
  61. //如果有检索数据的需求,用双列(Map)的容器,若没有用单列的(List)  
  62. //模拟数据库  
  63. class Db{  
  64.     private static Map<String,Book> map=new LinkedHashMap();//HashMap取出来和存进去的顺序不是一致的(自己测试)  
  65.     //类初始化代码(写在静态代码块里)  
  66.     static{  
  67.         map.put("1",new Book("1","JAVAWEB开发","老张","一本好书!"));  
  68.         map.put("2",new Book("2","JDBC开发","老张","一本好书!"));  
  69.         map.put("3",new Book("3","Spring开发","老黎","一本好书!"));  
  70.         map.put("4",new Book("4","struts开发","老毕","一本好书!"));  
  71.         map.put("5",new Book("5","android开发","老黎","一本好书!"));  
  72.     }  
  73.       
  74.     public static Map getAll(){  
  75.         return map;  
  76.     }  
  77. }  
  78.   
  79.   
  80. class Book{  
  81.     private String id;  
  82.     private String name;  
  83.     private String author;  
  84.     private String description;  
  85.       
  86.     public Book(){  
  87.         super();  
  88.     }  
  89.     public Book(String id, String name, String author, String description) {  
  90.         super();  
  91.         this.id = id;  
  92.         this.name = name;  
  93.         this.author = author;  
  94.         this.description = description;  
  95.     }  
  96.     public String getId() {  
  97.         return id;  
  98.     }  
  99.     public void setId(String id) {  
  100.         this.id = id;  
  101.     }  
  102.     public String getName() {  
  103.         return name;  
  104.     }  
  105.     public void setName(String name) {  
  106.         this.name = name;  
  107.     }  
  108.     public String getAuthor() {  
  109.         return author;  
  110.     }  
  111.     public void setAuthor(String author) {  
  112.         this.author = author;  
  113.     }  
  114.     public String getDescription() {  
  115.         return description;  
  116.     }  
  117.     public void setDescription(String description) {  
  118.         this.description = description;  
  119.     }  
  120.       
  121.       
  122. }  

CookieDemo4.java是每件商品的详细信息Servlet,在这里,cookie中的"BookHistory"就是存储的浏览记录,记录的是
浏览每件商品的id记录顺序的字符串。组建该字符串的时候需要注意一下四种情况:
1.当一件商品都没有浏览过时,即BookHistory=null 
此时加入的商品直接组成字符串CookieValue即可,假设此时浏览过id=1的商品,
CookieValue="1";

2.当浏览的商品是以前浏览过的,即BookHistory="2,5,1"的时候此时又浏览了id=1的

商品,这个时候,就要找到原来的那个id,删除它,将此次浏览的记录加入到最前面。

即BookHistory="2,5,1"变成了BookHistory="1,2,5"


3.由于我们的商品浏览记录有限,只记录前三个,所以每当新的记录加入的时候,要判断
是否满三个,满了的话,删除最后一个,将最新的加入到最前面。
如BookHistory=2,5,4,当又浏览了1商品,要改成BookHistory=1,2,5

4.如果不满3个,又不和以前的重复,直接将记录加在最前面即可
如BookHistory=2,5的时候,又浏览了1商品,此时BookHistory=1,2,5

CookieDemo4.java
  1. package cn.edu.cookie;  
  2.   
  3.   
  4. import java.io.IOException;  
  5. import java.io.PrintWriter;  
  6. import java.util.Arrays;  
  7. import java.util.LinkedList;  
  8.   
  9.   
  10. import javax.servlet.ServletException;  
  11. import javax.servlet.http.Cookie;  
  12. import javax.servlet.http.HttpServlet;  
  13. import javax.servlet.http.HttpServletRequest;  
  14. import javax.servlet.http.HttpServletResponse;  
  15. //显示商品详细信息的Servlet  
  16. public class CookieDemo4 extends HttpServlet {  
  17.   
  18.   
  19.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  20.             throws ServletException, IOException {  
  21.         response.setCharacterEncoding("UTF-8");  
  22.         response.setContentType("text/html;charset=UTF-8");  
  23.         PrintWriter out=response.getWriter();  
  24.           
  25.         //根据用户带过来的id,显示相应商品的详细信息  
  26.         //getParameter是拿到客户机带过来的数据(通过表单和链接传递的参数使用getParameter)  
  27.         String id =request.getParameter("id");//当两个Web组件之间为转发关系时采用getAttribute  
  28.         Book book=(Book) Db.getAll().get(id);  
  29.   
  30.   
  31.         out.write(book.getId()+"<br/>");  
  32.         out.write(book.getAuthor()+"<br/>");  
  33.         out.write(book.getName()+"<br/>");  
  34.         out.write(book.getDescription()+"<br/>");  
  35.           
  36.         //构建cookie,回写给浏览器  
  37.           
  38.         /*一个web应用最多能被浏览器记录20个左右的cookie 
  39.                            所以这里要把大量的cookie记录到一起,记录成一个cookie*/  
  40.         String CookieValue =buildCookie(id,request);  
  41.         Cookie cookie=new Cookie("BookHistory",CookieValue);  
  42.         cookie.setMaxAge(1*30*24*3600);  
  43.         cookie.setPath("/day07");  
  44.         response.addCookie(cookie);  
  45.     }  
  46.   
  47.   
  48.     private String buildCookie(String id, HttpServletRequest request) {  
  49.           
  50.         //四种cookie情况  
  51.         //BookHistory=null   1   1  
  52.         //BookHistory=2,5,1   1   1,2,5  
  53.         //BookHistory=2,5,4   1   1,2,5  
  54.         //BookHistory=2,5   1   1,2,5  
  55.           
  56.         String BookHistory=null;  
  57.         Cookie cookies[]=request.getCookies();  
  58.         for(int i=0;cookies!=null&&i<cookies.length;i++){  
  59.             if(cookies[i].getName().equals("BookHistory")){  
  60.                 BookHistory=cookies[i].getValue();  
  61.             }  
  62.         }  
  63.           
  64.         if(BookHistory==null){  
  65.             return id;  
  66.         }  
  67.           
  68.         //分割成数组,之后将数组转成集合,集合转成链表  
  69.         LinkedList<String> list=new LinkedList(Arrays.asList(BookHistory.split("\\,")));  
  70.         //分情况分配新的cookie值  
  71.         if(list.contains(id)){  
  72.             //BookHistory=2,5,1   1   1,2,5  
  73.             list.remove(id);  
  74.             list.addFirst(id);  
  75.         }else{  
  76.             if(list.size()>=3){  
  77.                 //BookHistory=2,5,4   1   1,2,5  
  78.                 list.removeLast();  
  79.                 list.addFirst(id);  
  80.             }else{  
  81.                 //BookHistory=2,5   1   1,2,5  
  82.                 //BookHistory=null   1   1  
  83.                 list.addFirst(id);  
  84.             }  
  85.         }  
  86.           
  87.         StringBuffer sb=new StringBuffer();  
  88.         for(String bid : list){  
  89.             sb.append(bid+",");  
  90.         }  
  91.           
  92.         return sb.deleteCharAt(sb.length()-1).toString();  
  93.     }  
  94.   
  95.   
  96.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  97.             throws ServletException, IOException {  
  98.         doGet(request,response);  
  99.     }  
  100.   
  101.   
  102. }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值