利用Cookie来实现显示商品浏览历史纪录

购物网站上使用来显示顾客浏览商品后的历史记录。下面的代码就是使用了Cookie技术来实现的。

第一个servle是用来输出网站的所有商品和显示最近浏览过什么商品:

  1 package com.bird.cookie;

  2 

  3 import java.io.IOException;

  4 import java.io.PrintWriter;

  5 import java.util.LinkedHashMap;

  6 import java.util.Map;

  7 

  8 import javax.servlet.ServletException;

  9 import javax.servlet.http.Cookie;

 10 import javax.servlet.http.HttpServlet;

 11 import javax.servlet.http.HttpServletRequest;

 12 import javax.servlet.http.HttpServletResponse;

 13 

 14 /**

 15  * @category 网站的首页,用来显示所有的信息

 16  * @author Wilbert

 17  *

 18  */

 19 public class CookieDemo2 extends HttpServlet {

 20 

 21     private static final long serialVersionUID = 1L;

 22 

 23     public void doGet(HttpServletRequest request, HttpServletResponse response)

 24             throws ServletException, IOException {

 25         //解决乱码问题

 26         response.setCharacterEncoding("UTF-8");

 27         response.setContentType("text/html;charset=UTF-8");

 28         PrintWriter out = response.getWriter();

 29         

 30         

 31         

 32         //1.输出网站的所有商品

 33         out.write("本网站有如下商品<br/>");

 34         Map<String,Book> map = DB.getAll();

 35         for(Map.Entry<String, Book> entry : map.entrySet()){

 36             Book book = entry.getValue();

 37             out.print("<a href='/WebDemo/servlet/CookieDemo3?id="+book.getId()+"' target='_blank'>"+book.getName()+"</a><br/>");

 38         }

 39         

 40         //2.显示用户看过的商品

 41         out.write("<br/>您曾经看过如下商品<br/>");

 42         Cookie cookies[] = request.getCookies();

 43         for(int i = 0; cookies!=null && i < cookies.length; i++){

 44             if(cookies[i].getName().equals("bookHistory")){

 45                 String []ids = cookies[i].getValue().split("\\,");

 46                 for(String s : ids){

 47                     Book book = DB.getAll().get(s);

 48                     out.write(book.getName()+"<br/>");

 49                 }

 50             }

 51         }

 52     }

 53    

 54     public void doPost(HttpServletRequest request, HttpServletResponse response)

 55             throws ServletException, IOException {

 56 

 57     }

 58 

 59 }

 60 

 61 class DB{

 62     //必须使用链表Map进行有序存储

 63     private static Map<String,Book> map = new LinkedHashMap<String,Book>();

 64     static{

 65         map.put("1", new Book("1","javaweb开发详解","老张","一本好书!!!"));

 66         map.put("2", new Book("2","jdbc开发详解","老张","一本好书!!!"));

 67         map.put("3", new Book("3","sping开发详解","老黎","一本好书!!!"));

 68         map.put("4", new Book("4","struts开发详解","老宇","一本好书!!!"));

 69         map.put("5", new Book("5","heibrate开发详解","老毕","一本好书!!!"));

 70     }

 71     

 72     public static Map<String,Book> getAll(){

 73         return map;

 74     }

 75 }

 76 class Book{

 77     private String id;

 78     private String name;

 79     private String author;

 80     private String description;

 81     

 82     public Book(){}

 83     

 84     public Book(String id, String name, String author, String description) {

 85         this.id = id;

 86         this.name = name;

 87         this.author = author;

 88         this.description = description;

 89     }

 90     

 91     public String getId() {

 92         return id;

 93     }

 94     public void setId(String id) {

 95         this.id = id;

 96     }

 97     public String getName() {

 98         return name;

 99     }

100     public void setName(String name) {

101         this.name = name;

102     }

103     public String getAuthor() {

104         return author;

105     }

106     public void setAuthor(String author) {

107         this.author = author;

108     }

109     public String getDescription() {

110         return description;

111     }

112     public void setDescription(String description) {

113         this.description = description;

114     }

115     

116     

117 }

 

第二个Servlet是用来显示详细的商品信息的:

 1 package com.bird.cookie;

 2 

 3 import java.io.IOException;

 4 import java.io.PrintWriter;

 5 import java.util.Arrays;

 6 import java.util.LinkedList;

 7 

 8 import javax.servlet.ServletException;

 9 import javax.servlet.http.Cookie;

10 import javax.servlet.http.HttpServlet;

11 import javax.servlet.http.HttpServletRequest;

12 import javax.servlet.http.HttpServletResponse;

13 

14 /**

15  * @category 显示商品的详细信息

16  * @author Wilbert

17  *

18  */

19 public class CookieDemo3 extends HttpServlet {

20 

21     private static final long serialVersionUID = 1L;

22 

23     public void doGet(HttpServletRequest request, HttpServletResponse response)

24             throws ServletException, IOException {

25         response.setCharacterEncoding("UTF-8");

26         response.setContentType("text/html;charset=UTF-8");

27         PrintWriter out = response.getWriter();

28         

29         //1.根据用户提交的id显示对应的商品的详细的信息

30         

31         String id = request.getParameter("id");

32         Book book = DB.getAll().get(id);

33         out.write("id为"+book.getId()+"<br/>");

34         out.write("书名为"+book.getName()+"<br/>");

35         out.write("作者为"+book.getAuthor()+"<br/>");

36         out.write("描述为"+book.getDescription()+"<br/>");

37         

38         //2.构建Cookie,回写给浏览器

39         String cookieValue = buildCookie(id,request);

40         Cookie cookie = new Cookie("bookHistory",cookieValue);

41         cookie.setMaxAge(30*24*60*60);

42         cookie.setPath("/WebDemo");

43         response.addCookie(cookie);

44     }

45 

46     private String buildCookie(String id, HttpServletRequest request) {

47         String bookHisttory = null;

48         Cookie [] cookies = request.getCookies();

49         for(int i = 0; cookies!=null && i < cookies.length; i++){

50             bookHisttory = cookies[i].getValue();

51         }

52         

53         if(bookHisttory==null)

54             return id;

55         

56         LinkedList<String> list = new LinkedList<String>(Arrays.asList(bookHisttory.split("\\,")));

57         if(list.contains(id)){

58             list.remove(id);

59         }else{

60             if(list.size()>=3){

61                 list.removeLast();

62             }

63         }

64         list.addFirst(id);

65         

66         StringBuffer sb = new StringBuffer();

67         for(String bid: list){

68             sb.append(bid + ",");

69         }

70         return sb.deleteCharAt(sb.length()-1).toString();//删除最后多余 的一个逗号 

71     }

72 

73     public void doPost(HttpServletRequest request, HttpServletResponse response)

74             throws ServletException, IOException {

75 

76     }

77 

78 }

 

 

 

你可能感兴趣的:(cookie)