首页 > 代码库 > Cookie案例-显示商品浏览历史纪录

Cookie案例-显示商品浏览历史纪录

 1 package cn.itcast.cookie; 2  3 import java.io.IOException; 4 import java.io.PrintWriter; 5 import java.util.Date; 6 import java.util.HashMap; 7 import java.util.LinkedHashMap; 8 import java.util.Map; 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 16 import cn.itcast.Book;17 import cn.itcast.Db;18 19 public class CookieDemo extends HttpServlet {20 21     public void doGet(HttpServletRequest request, HttpServletResponse response)22             throws ServletException, IOException {23 24         response.setCharacterEncoding("UTF-8");25         response.setContentType("text/html;charset=UTF-8");26         PrintWriter out = response.getWriter();27 28         // 1.输出网站的商品29         out.write("本网站有如下商品:<br/>");30         Db db = new Db();31         Map<String, Book> map = db.getAll();32 33         for (Map.Entry<String, Book> entry : map.entrySet()) {34             Book book = entry.getValue();35             out.print("<a href=http://www.mamicode.com/‘/ServletDemo/servlet/CookieDemo2?id="36                     + book.getId() + "‘target=‘_blank‘>" + book.getName()37                     + "</a><br/>");38         }39 40         // 2.显示用户曾经看过的商品41         out.print("<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 id:ids){47                     Book book = (Book) Db.getAll().get(id);48                     out.print(book.getName()+"<br/>");49                 }50             }51         }52 53     }54 55     public void doPost(HttpServletRequest request, HttpServletResponse response)56             throws ServletException, IOException {57 58     }59 60 }
View Code
 1 package cn.itcast.cookie; 2  3 import java.io.IOException; 4 import java.io.PrintWriter; 5 import java.util.Arrays; 6 import java.util.LinkedList; 7 import java.util.List; 8  9 import javax.servlet.ServletException;10 import javax.servlet.http.Cookie;11 import javax.servlet.http.HttpServlet;12 import javax.servlet.http.HttpServletRequest;13 import javax.servlet.http.HttpServletResponse;14 15 import cn.itcast.Book;16 import cn.itcast.Db;17 18 public class CookieDemo2 extends HttpServlet {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.给据用户带来的id,显示相应商品的详细信息。28         String id = request.getParameter("id");29         Book book = (Book) Db.getAll().get(id);30         out.write(book.getId() + "<br/>");31         out.write(book.getName() + "<br/>");32         out.write(book.getAuthor() + "<br/>");33         out.write(book.getDescription() + "<br/>");34 35         // 2.构建cookie,会写给浏览器36         String cookieValue =http://www.mamicode.com/ buildCookie(id, request);37         Cookie cookie = new Cookie("bookHistory", cookieValue);38         cookie.setMaxAge(1 * 30 * 24 * 60 * 60);39         cookie.setPath("/ServletDemo");40         response.addCookie(cookie);41     }42 43     private String buildCookie(String id, HttpServletRequest request) {44 45         String bookHistory = null;46         Cookie cookies[] = request.getCookies();47         for (int i = 0; cookies != null && i < cookies.length; i++) {48             if (cookies[i].getName().equals("bookHistory")) {49                 bookHistory = cookies[i].getValue();50             }51 52         }53 54         if (bookHistory == null) {55             return id;56         }57 58         LinkedList<String> list = new LinkedList(Arrays59                 .asList(bookHistory.split("\\,")));60         61         if (list.contains(id)) {62             list.remove(id);63         } else {64             if (list.size() >= 3) {65                 list.removeLast();66             }67         }68         69         list.addFirst(id);70         71         StringBuffer sb = new StringBuffer();72         for (String bid : list) {73             sb.append(bid+",");74         }75         76         return sb.deleteCharAt(sb.length()-1).toString();77     }78 79     public void doPost(HttpServletRequest request, HttpServletResponse response)80             throws ServletException, IOException {81 82     }83 84 }
View Code
 1 package cn.itcast; 2  3 import java.util.LinkedHashMap; 4 import java.util.Map; 5  6 public class Db { 7     private static Map<String, Book> map = new LinkedHashMap(); 8      9     static{10         map.put("1", new Book("1","javaweb开发","Zero","一本好书!!"));11         map.put("2", new Book("2","jdbc开发","one","一本好书!!"));12         map.put("3", new Book("3","spring开发","two","一本好书!!"));13         map.put("4", new Book("4","struks开发","three","一本好书!!"));14         map.put("5", new Book("5","hibernate开发","four","一本好书!!"));15     }16     17     public static Map getAll(){18         return map;19     }20 }
View Code
 1 package cn.itcast; 2  3  4 public class Book { 5     private String id; 6     private String name; 7     private String author; 8     private String description; 9     10     public Book(String id, String name, String author, String description) {11         super();12         this.id = id;13         this.name = name;14         this.author = author;15         this.description = description;16     }17 18     public String getId() {19         return id;20     }21 22     public void setId(String id) {23         this.id = id;24     }25 26     public String getName() {27         return name;28     }29 30     public void setName(String name) {31         this.name = name;32     }33 34     public String getAuthor() {35         return author;36     }37 38     public void setAuthor(String author) {39         this.author = author;40     }41 42     public String getDescription() {43         return description;44     }45 46     public void setDescription(String description) {47         this.description = description;48     }49 }
View Code