1.
package cn.itcast.cookie; import java.io.IOException; import java.io.PrintWriter; import java.util.LinkedHashMap; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; //代表首页的servlet public class CookieDemo3 extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1. 输出网站所有的商品 request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); PrintWriter writer = response.getWriter(); writer.write("本网站有如下商品:<br/>"); // 浏览器换行用br,java用/r/n //1.2 从DB中获取所有的商品信息 Map<String, Book> map = DB.getAll(); for(Map.Entry<String, Book> entry : map.entrySet()){ Book book = entry.getValue(); writer.print("<a href='/day07/CookieDemo4?id="+ book.getId() +" ' target='_blank'>"+ book.getName() +"</a><br/>"); } //2.显示用户曾经看过的商品 writer.print("<br/>您曾经浏览过的商品: <br/>"); Cookie[] cookies = request.getCookies(); for(int i = 0; cookies != null && i < cookies.length; i++){ if (cookies[i].getName().equals("bookHistory")) { String[] ids = cookies[i].getValue().split("\,"); //推荐转意 for( String id : ids) { // 通过id去db中查找对应的书 Book book = DB.getAll().get(id); writer.print(book.getName() + "<br/>"); } } } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException ,IOException { this.doGet(request, response); } } class DB{ //想存的数据的顺序与显示的顺序一致,用LinkedHashMap private static Map<String, Book> map = new LinkedHashMap<String, Book>(); static{ //静态代码块确保只加载一次 map.put("1", new Book("1", "javaweb开发", "老张", "高薪书")); map.put("2", new Book("2", "andorid开发", "老张", "高薪书")); map.put("3", new Book("3", "ios开发", "老张", "高薪书")); map.put("4", new Book("4", "javaEE开发", "老张", "高薪书")); map.put("5", new Book("5", "疯狂的java开发", "老张", "高薪书")); } public static Map<String, Book> getAll(){ return map; } } class Book{ private String id; private String name; private String author; private String description; public Book(){} public Book(String id, String name, String author, String description) { super(); this.id = id; this.name = name; this.author = author; this.description = description; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }
2.
package cn.itcast.cookie; import java.io.IOException; import java.io.PrintWriter; import java.util.Arrays; import java.util.LinkedList; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class CookieDemo4 extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); //根据用户传过来的id,显示相应商品的详细信息 String id = request.getParameter("id"); Book book = DB.getAll().get(id); out.write(book.getId() + "<br/>"); out.write(book.getName() + "<br/>"); out.write(book.getAuthor() + "<br/>"); out.write(book.getDescription() + "<br/>"); //构建cookie, 回写给浏览器 String cookieValue = buildCookie(id, request); Cookie cookie = new Cookie("bookHistory", cookieValue); cookie.setMaxAge(3600*24*30); cookie.setPath(request.getContextPath()); response.addCookie(cookie); } private String buildCookie(String id, HttpServletRequest request) { //bookHistory = null 1 1 //bookHistory = 2,5,1 1 1,2,5 //bookHistory = 2,5,4 1 1,2,5 //bookHistory = 2,5 1 1,2,5 Cookie[] cookies = request.getCookies(); String bookHistory = null; for(int i = 0; cookies != null && i < cookies.length; i++) { if(cookies[i].getName().equals("bookHistory")){ bookHistory = cookies[i].getValue(); } } if(bookHistory == null){ //第一次 return id; } LinkedList<String> list = new LinkedList<String>(Arrays.asList(bookHistory.split("\,"))); /*if(list.contains(id)){ //当前浏览的商品在cookie中存在 list.remove(id); //把存在的删除 list.addFirst(id);//把当前的放在第一个 } else{ //不存在; if(list.size() >= 3){// 如何list满了把当前的放在第一个并删除最后一个 list.removeLast(); list.addFirst(id); } else{ //直接添加到list头部 list.addFirst(id); } }*/ if(list.contains(id)){ list.remove(id); } else{ if(list.size() >= 3){ list.removeLast(); } } list.addFirst(id); StringBuilder sb = new StringBuilder(); for(String bookId : list) { sb.append(bookId).append(","); } sb.deleteCharAt(sb.length() - 1); bookHistory = sb.toString(); return bookHistory; } }