Cookie应用

CookieDemo1

  1 import java.io.IOException;
  2 import java.io.PrintWriter;
  3 import java.util.LinkedHashMap;
  4 import java.util.Map;
  5 
  6 import javax.servlet.ServletException;
  7 import javax.servlet.http.Cookie;
  8 import javax.servlet.http.HttpServlet;
  9 import javax.servlet.http.HttpServletRequest;
 10 import javax.servlet.http.HttpServletResponse;
 11 
 12 import sun.security.action.PutAllAction;
 13 
 14 import com.sun.faces.config.beans.LifecycleBean;
 15 
 16 public class CookieDemo1 extends HttpServlet {
 17 
 18     public void doGet(HttpServletRequest request, HttpServletResponse response)
 19             throws ServletException, IOException {
 20         response.setCharacterEncoding("UTF-8");
 21         response.setContentType("text/html;charset=UTF-8");
 22         PrintWriter out = response.getWriter();
 23         
 24         //1.输出网站所有商品
 25         out.print("本站的所有商品:<br/>");
 26         //模拟一个数据库输出
 27         Map<String, Book> map = Db.getAll();
 28         //增强for循环迭代map
 29         for(Map.Entry<String,Book> entry:map.entrySet()){
 30             Book book = entry.getValue();
 31             out.print("<a href='/test01/servlet/CookieDemo2?id="+book.getId()+"' target='_blank'>"+book.getName()+"</a><br/>");
 32         }
 33         
 34         //2.显示用户曾经看过的商品
 35         out.print("<br/>你曾经看过的商品:<br/>");
 36         Cookie[] cookies = request.getCookies();
 37         for(int i = 0;cookies!=null && i<cookies.length;i++){
 38             if(cookies[i].getName().equals("bookHistory")){
 39                 String[] ids = cookies[i].getValue().split("\\,");
 40                 //增强for循环
 41                 for(String id:ids){
 42                     Book book =map.get(id);
 43                     out.print(book.getName()+"<br/>");
 44                 }
 45             }
 46         }
 47     }
 48 
 49     public void doPost(HttpServletRequest request, HttpServletResponse response)
 50             throws ServletException, IOException {
 51 
 52     }
 53 }
 54 class Db{
 55     private static Map<String, Book> map = new LinkedHashMap<String,Book>();
 56     static{
 57         map.put("1", new Book("1", "水浒传", "施耐庵", "good"));
 58         map.put("2", new Book("2", "西游记", "吴承恩", "good"));
 59         map.put("3", new Book("3", "三国演义", "罗贯中", "good"));
 60         map.put("4", new Book("4", "红楼梦", "曹雪芹", "good"));
 61     }
 62     public  static Map<String, Book> getAll(){
 63         return map;
 64     }
 65 }
 66 class Book{
 67     private String id;
 68     private String name;
 69     private String author;
 70     private String description;
 71     
 72     public Book(String id, String name, String author, String description) {
 73         super();
 74         this.id = id;
 75         this.name = name;
 76         this.author = author;
 77         this.description = description;
 78     }
 79 
 80     public Book() {
 81         super();
 82         // TODO Auto-generated constructor stub
 83     }
 84 
 85     public String getId() {
 86         return id;
 87     }
 88 
 89     public void setId(String id) {
 90         this.id = id;
 91     }
 92 
 93     public String getName() {
 94         return name;
 95     }
 96 
 97     public void setName(String name) {
 98         this.name = name;
 99     }
100 
101     public String getAuthor() {
102         return author;
103     }
104 
105     public void setAuthor(String author) {
106         this.author = author;
107     }
108 
109     public String getDescription() {
110         return description;
111     }
112 
113     public void setDescription(String description) {
114         this.description = description;
115     }
116     
117     
118 }

CookieDemo2

 1 import java.awt.image.RescaleOp;
 2 import java.io.IOException;
 3 import java.io.PrintWriter;
 4 import java.util.Arrays;
 5 import java.util.LinkedList;
 6 
 7 import javax.servlet.ServletException;
 8 import javax.servlet.http.Cookie;
 9 import javax.servlet.http.HttpServlet;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12 
13 //显示商品详细信息
14 public class CookieDemo2 extends HttpServlet {
15 
16     public void doGet(HttpServletRequest request, HttpServletResponse response)
17             throws ServletException, IOException {
18         // 中文乱码
19         response.setCharacterEncoding("UTF-8");
20         response.setContentType("text/html;charset=UTF-8");
21         PrintWriter out = response.getWriter();
22 
23         // 显示商品详细信息,先得到商品id
24         String id = request.getParameter("id");
25         Book book = Db.getAll().get(id);
26 
27         out.print(book.getId() + "<br/>");
28         out.print(book.getName() + "<br/>");
29         out.print(book.getAuthor() + "<br/>");
30         out.print(book.getDescription() + "<br/>");
31 
32         // 构建Cookie,回写给浏览器
33         String cookieValue = bulidCookieValue(id, request);
34         Cookie cookie = new Cookie("bookHistory", cookieValue);
35         cookie.setMaxAge(1 * 30 * 24 * 60 * 60);
36         cookie.setPath("/test01");
37         response.addCookie(cookie);
38     }
39 
40     private String bulidCookieValue(String id, HttpServletRequest request) {
41         
42         //bookHistory = null   1    1
43         //bookHistory = 2,5,1  1    1,2,5
44         //bookHistory = 2,5,4  1    1,2,5
45         //bookHistory = 2,5    1    1,2,5
46                 
47         String bookHistory = null;
48         Cookie[] cookies = request.getCookies();
49         for(int i = 0;cookies!=null && i<cookies.length;i++){
50             if(cookies[i].getName().equals("bookHistory")){
51                 bookHistory=cookies[i].getValue();
52             }
53         }
54         
55         if(bookHistory==null){
56             //bookHistory = null    1    1
57             return id;
58         }
59         
60         LinkedList list = new LinkedList(Arrays.asList(bookHistory.split("\\,")));
61         if(list.contains(id)){
62             //bookHistory = 2,5,1  1    1,2,5
63             list.remove(id);
64             list.addFirst(id);
65         }else{
66             if(list.size()>=3){
67                 //bookHistory = 2,5,4  1    1,2,5
68                 //bookHistory = 2,5    1    1,2,5
69                 list.removeLast();
70                 list.addFirst(id);
71             }else{
72                 list.addFirst(id);
73             }
74         }
75         
76         StringBuffer sb = new StringBuffer();
77         for(int i=0;i<list.size();i++){
78             sb.append(list.get(i)+",");
79         }
80         
81         return sb.deleteCharAt(sb.length()-1).toString();
82     }
83 
84     public void doPost(HttpServletRequest request, HttpServletResponse response)
85             throws ServletException, IOException {
86 
87     }
88 
89 }

 

posted @ 2017-03-12 15:06  drinkMilk  阅读(345)  评论(0编辑  收藏  举报