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("本网站有如下书籍:"); Set> set=DB.getAll().entrySet(); for(Map.Entry me : set){ Book book= me.getValue(); out.write(" "+book.getName()+""); out.write(" "); } //2.显示用户曾经浏览过的商品 out.write(" 您曾经浏览过的商品: " ); Cookie cookies[] = request.getCookies(); for(int i=0;cookies!=null && i "); } } } } 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("您要查看的书的详细信息如下:"); out.write(book.getId() + ""); out.write(book.getName() + ""); out.write(book.getAuthor() + ""); out.write(book.getDescription() + ""); //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&&ilist = 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 Mapmap = 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; } }