使用Cookie實現用戶商品歷史瀏覽記錄


該功能分為四個模塊:

1. 獲取所有商品並以鏈接的形式顯示

1 out.write("網站商品: <br/>");
2 Map<String, Book> books = Db.getAll();
3 for (Map.Entry<String, Book> me : books.entrySet()) {
4     String id = me.getKey();
5     Book book = me.getValue();
6     out.write("<a href='/test/servlet/CookieDemo3?id="+ id +"'>"+ book.getName() +"</a><br/>");
7 }

模擬數據庫和用戶實體

 1 // 模擬數據庫
 2 class Db {
 3 
 4     // 要求: 1有序 2查找 -> LinkedHashMap
 5     private static Map<String, Book> map = new LinkedHashMap<String, Book>();
 6 
 7     // 初始化map
 8     static {
 9         map.put("1", new Book("JavaWeb秘籍", 12.45));
10         map.put("2", new Book("Spring開發", 45.5));
11         map.put("3", new Book("SpringMVC開發", 82.45));
12         map.put("4", new Book("Mybatis開發", 75.5));
13     }
14 
15     public static Map<String, Book> getAll() {
16         return map;
17     }
18 }
19 
20 // 商品實體
21 class Book {
22 
23     private String name;
24     private double price;
25 
26     public Book(String name, double price) {
27         this.name = name;
28         this.price = price;
29     }
30 
31     public String getName() {
32         return name;
33     }
34 
35     public void setName(String name) {
36         this.name = name;
37     }
38 
39     public double getPrice() {
40         return price;
41     }
42 
43     public void setPrice(double price) {
44         this.price = price;
45     }
46 }
View Code

2. 顯示用戶上次瀏覽過的商品

通過用戶攜帶的cookie顯示用戶歷史瀏覽記錄

 1 out.write("<br/>上次瀏覽過的商品: <br/>");
 2 Cookie[] cookies = req.getCookies();
 3 for (int i = 0; cookies != null && i < cookies.length; i++) {
 4     if (cookies[i].getName().equals("bookhistory")) {
 5         String bookhistoryValue = cookies[i].getValue();  // 1_4_3
 6         String[] ids = bookhistoryValue.split("\\_");  // [1, 4, 3]
 7         // 迭代瀏覽過的商品id
 8         for (String id : ids) {
 9             out.write(Db.getAll().get(id).getName() + "<br/>");
10         }
11     }
12 }

說明: 第一步和第二步可以做成同一個servlet中, 完整代碼:

 1 /**
 2  * Created by IntelliJ IDEA.
 3  *
 4  * @Auther: ShaoHsiung
 5  * @Date: 2018/8/28 10:12
 6  * @Title: 顯示用戶上次瀏覽商品
 7  * @Description: 主頁
 8  */
 9 public class CookieDemo2 extends HttpServlet {
10     @Override
11     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
12 
13         // 設置瀏覽器編碼
14         resp.setContentType("text/html; charset=utf-8");
15 
16         Writer out = resp.getWriter();
17 
18         // 獲取所有商品並以鏈接的形式顯示
19         out.write("網站商品: <br/>");
20         Map<String, Book> books = Db.getAll();
21         for (Map.Entry<String, Book> me : books.entrySet()) {
22             String id = me.getKey();
23             Book book = me.getValue();
24             out.write("<a href='/test/servlet/CookieDemo3?id="+ id +"'>"+ book.getName() +"</a><br/>");
25         }
26 
27         // 顯示用戶上次瀏覽過的商品
28         out.write("<br/>上次瀏覽過的商品: <br/>");
29         Cookie[] cookies = req.getCookies();
30         for (int i = 0; cookies != null && i < cookies.length; i++) {
31             if (cookies[i].getName().equals("bookhistory")) {
32                 String bookhistoryValue = cookies[i].getValue();  // 1_4_3
33                 String[] ids = bookhistoryValue.split("\\_");  // [1, 4, 3]
34                 // 迭代瀏覽過的商品id
35                 for (String id : ids) {
36                     out.write(Db.getAll().get(id).getName() + "<br/>");
37                 }
38             }
39         }
40     }
41 
42     @Override
43     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
44         doGet(req, resp);
45     }
46 }
47 
48 // 模擬數據庫
49 class Db {
50 
51     // 要求: 1有序 2查找 -> LinkedHashMap
52     private static Map<String, Book> map = new LinkedHashMap<String, Book>();
53 
54     // 初始化map
55     static {
56         map.put("1", new Book("JavaWeb秘籍", 12.45));
57         map.put("2", new Book("Spring開發", 45.5));
58         map.put("3", new Book("SpringMVC開發", 82.45));
59         map.put("4", new Book("Mybatis開發", 75.5));
60     }
61 
62     public static Map<String, Book> getAll() {
63         return map;
64     }
65 }
66 
67 // 商品實體
68 class Book {
69 
70     private String name;
71     private double price;
72 
73     public Book(String name, double price) {
74         this.name = name;
75         this.price = price;
76     }
77 
78     public String getName() {
79         return name;
80     }
81 
82     public void setName(String name) {
83         this.name = name;
84     }
85 
86     public double getPrice() {
87         return price;
88     }
89 
90     public void setPrice(double price) {
91         this.price = price;
92     }
93 }
View Code

3. 顯示商品詳細信息

通過請求參數在數據庫中查詢商品

1 out.write("商品詳細信息: <br/>");
2 String id = req.getParameter("id");
3 Book book = Db.getAll().get(id);
4 out.write(book.getName() + "<br/>");
5 out.write(book.getPrice() + "<br/>");

4. 將商品的id添加到cookie中並返回給用戶

這里使用makeCookie()方法封裝商品歷史記錄cookie的制作, 這部分主要任務就是將cookie返回給瀏覽器

1 String bookhistoryValue = makeCookie(req, id);
2 Cookie bookhistory = new Cookie("bookhistory", bookhistoryValue);
3 bookhistory.setMaxAge(3600);
4 bookhistory.setPath(this.getServletContext().getContextPath());
5 resp.addCookie(bookhistory);

5. 完成makeCookie()方法

使用字符串拼接的方式產生cookie的具體步驟, 特別需要注意四種可能獲取到的cookie及其處理方式.

注意: 四種可能獲取到的cookie:

獲取到的cookie    查看商品   返回的cookie
null 1 1
1_2_3 4 4_1_2
1_2_3 2 2_1_3
1_2 4 1_2_4
 
        
 1 private String makeCookie(HttpServletRequest req, String id) {
 2     String bookhistoryValue = null;
 3     Cookie[] cookies = req.getCookies();
 4     for (int i = 0; cookies != null && i < cookies.length; i++) {
 5         if (cookies[i].getName().equals("bookhistory")) {
 6             bookhistoryValue = cookies[i].getValue();
 7         }
 8     }
 9 
10     // null            1          1
11     if (bookhistoryValue == null) {
12         return id;
13     }
14     List l = Arrays.asList(bookhistoryValue.split("\\_"));
15     // 轉化為鏈表方便操作, 提供addFirst和removeLast等方法
16     LinkedList<String> list = new LinkedList();
17     list.addAll(l);
18     if (list.contains(id)) {
19         // 1_2_3           2        2_1_3
20         list.remove(id);
21         list.addFirst(id);
22     } else {
23         // 1_2_3           4        4_1_2
24         if (list.size() == 3) {
25             list.removeLast();
26             list.addFirst(id);
27         } else {
28             // 1_2             4        1_2_4
29             list.addFirst(id);
30         }
31     }
32     StringBuffer buffer = new StringBuffer();
33     for (String newId : list) {
34         buffer.append(newId + "_");  // 1_5_3_
35     }
36     return buffer.deleteCharAt(buffer.length()-1).toString();  // 刪除最后一個下划線
37 }

說明: 同理, 第三步和第四步可以做成同一個servlet中, 完整代碼:

 1 /**
 2  * Created by IntelliJ IDEA.
 3  *
 4  * @Auther: ShaoHsiung
 5  * @Date: 2018/8/28 10:12
 6  * @Title: 顯示用戶上次瀏覽商品
 7  * @Description: 商品詳細信息頁面
 8  */
 9 public class CookieDemo3 extends HttpServlet {
10     @Override
11     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
12 
13         resp.setContentType("text/html; charset=utf-8");
14 
15         Writer out = resp.getWriter();
16 
17         // 顯示商品詳細信息
18         out.write("商品詳細信息: <br/>");
19         String id = req.getParameter("id");
20         Book book = Db.getAll().get(id);
21         out.write(book.getName() + "<br/>");
22         out.write(book.getPrice() + "<br/>");
23 
24         // 將商品的id添加到cookie中並返回給用戶
25         String bookhistoryValue = makeCookie(req, id);
26         Cookie bookhistory = new Cookie("bookhistory", bookhistoryValue);
27         bookhistory.setMaxAge(3600);
28         bookhistory.setPath(this.getServletContext().getContextPath());
29         resp.addCookie(bookhistory);
30     }
31 
32     private String makeCookie(HttpServletRequest req, String id) {
33 
34         String bookhistoryValue = null;
35 
36         Cookie[] cookies = req.getCookies();
37         for (int i = 0; cookies != null && i < cookies.length; i++) {
38             if (cookies[i].getName().equals("bookhistory")) {
39                 bookhistoryValue = cookies[i].getValue();
40             }
41         }
42 
43         /*
44             重點: 四種情況
45 
46          獲取到的cookie    查看商品   返回的cookie
47             null            1          1
48             1_2_3           4        4_1_2
49             1_2_3           2        2_1_3
50             1_2             4        1_2_4
51          */
52 
53         // null            1          1
54         if (bookhistoryValue == null) {
55             return id;
56         }
57 
58 
59         List l = Arrays.asList(bookhistoryValue.split("\\_"));
60         // 轉化為鏈表方便操作, 提供addFirst和removeLast等方法
61         LinkedList<String> list = new LinkedList();
62         list.addAll(l);
63         if (list.contains(id)) {
64             // 1_2_3           2        2_1_3
65             list.remove(id);
66             list.addFirst(id);
67         } else {
68             // 1_2_3           4        4_1_2
69             if (list.size() == 3) {
70                 list.removeLast();
71                 list.addFirst(id);
72             } else {
73                 // 1_2             4        1_2_4
74                 list.addFirst(id);
75             }
76         }
77 
78         StringBuffer buffer = new StringBuffer();
79         for (String newId : list) {
80             buffer.append(newId + "_");  // 1_5_3_
81         }
82 
83         return buffer.deleteCharAt(buffer.length()-1).toString();  // 刪除最后一個下划線
84     }
85 
86     @Override
87     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
88         doGet(req, resp);
89     }
90 }
View Code

效果圖:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM