1、CookieHistoryDemo1.java代码实现如下:
import java.io.IOException; import java.io.PrintWriter; import java.util.Map; import java.util.Set; 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 CookieHistoryDemo1 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); // 1.显示网站所有商品 out.write("本网站有如下书籍:<br><br>"); Set<Map.Entry<String, Book>> set=DB.getAll().entrySet(); for(Map.Entry<String, Book> me : set){ Book book= me.getValue(); out.write("<a href='/MyWebStart/servlet/CookieHistoryDemo2?id="+book.getId()+"' target='_blank'>"+book.getName()+"</a>"); out.write("<br/>"); } //2.显示用户曾经浏览过的商品 out.write("<br/><br/>您曾经浏览过的商品:<br/>" ); Cookie cookies[] = request.getCookies(); for(int i=0;cookies!=null && i<cookies.length;i++){ Cookie cookie = cookies[i]; if(cookie.getName().equals("bookHistory")){ String bookHistory = cookie.getValue(); // 2_3 String ids[] = bookHistory.split("\\_"); //[2,3] for(String id: ids){ Book book = (Book) DB.getAll().get(id); out.write(book.getName() + "<br/>"); } } } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
2,CookieHistoryDemo2.java
import java.io.IOException; import java.io.PrintWriter; import java.util.Arrays; import java.util.LinkedList; import java.util.List; 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 CookieHistoryDemo2 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); //1.根据用户带过来的id号,显示商品的详细信息 String id = request.getParameter("id"); Book book = (Book) DB.getAll().get(id); out.write("您要查看的书的详细信息如下:<br/><br/>"); out.write(book.getId() + "<br/>"); out.write(book.getName() + "<br/>"); out.write(book.getAuthor() + "<br/>"); out.write(book.getDescription() + "<br/>"); //2.给用户回送包含当前商品id的cookie String bookHistory = makeHistory(request,id); // 3_2 Cookie cookie = new Cookie("bookHistory",bookHistory); cookie.setMaxAge(1*30*24*3600); response.addCookie(cookie); } private String makeHistory(HttpServletRequest request, String id) { String bookHistory = null; Cookie cookies[] = request.getCookies(); for(int i=0;cookies!=null&&i<cookies.length;i++){ if(cookies[i].getName().equals("bookHistory")){ bookHistory = cookies[i].getValue(); } } // bookHistory=null 1 bookHistory=1 // bookHistory=3_1_5 1 bookHistory=1_3_5 // bookHistory=3_2_5 1 bookHistory=1_3_2 // bookHistory=3_2 1 bookHistory=1_3_2 // bookHistory=null 1 bookHistory=1 if(bookHistory==null){ return id; } List l = Arrays.asList(bookHistory.split("\\_")); //[3,4] //数组 链接 LinkedList<String> list = new LinkedList(); list.addAll(l); if(list.contains(id)){ // bookHistory=3_1_5 1 bookHistory=1_3_5 list.remove(id); list.addFirst(id); }else{ if(list.size()>=3){ // bookHistory=3_2_5 1 bookHistory=1_3_2 list.removeLast(); list.addFirst(id); }else{ // bookHistory=3_2 1 bookHistory=1_3_2 list.addFirst(id); } } StringBuffer sb = new StringBuffer(); //2_3_4 for(String lid: list){ sb.append(lid + "_"); } return sb.deleteCharAt(sb.length()-1).toString(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }
3,DB.java
import java.util.LinkedHashMap; import java.util.Map; public class DB { private static Map<String,Book> map = new LinkedHashMap(); static{ map.put("1", new Book("1","javaweb","Job","New book")); map.put("2", new Book("2","spring","Hitpop","Good book")); map.put("3", new Book("3","hibernate","Brucec","Nice book")); map.put("4", new Book("4","struts","Smith","Pass book")); map.put("5", new Book("5","ajax","Zhangli","Hand book")); } public static Map getAll(){ return map; } }
4,Book.java
public class Book { private String id; private String name; private String author; private String description; public Book() { super(); // TODO Auto-generated constructor stub } 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; } }