CookieDemo3.java: package cn.edu.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 { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); PrintWriter out=response.getWriter(); //1.输出网站所有商品 out.write("本网站有如下商品:<br/>"); Map<String,Book> map = Db.getAll(); for(Map.Entry<String, Book> entry :map.entrySet()) { Book book=entry.getValue(); out.print("<a href='/day07/CookieDemo4?id="+book.getId()+"' terget='_blank'>"+book.getName()+"</a><br/>"); } //2.输出用户曾经看过的商品 out.print("您曾经看过如下商品:<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){ Book book=(Book)Db.getAll().get(id); out.print(book.getName()+"<br/>"); } } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } } //如果有检索数据的需求,用双列(Map)的容器,若没有用单列的(List) //模拟数据库 class Db{ private static Map<String,Book> map=new LinkedHashMap();//HashMap取出来和存进去的顺序不是一致的(自己测试) //类初始化代码(写在静态代码块里) static{ map.put("1",new Book("1","JAVAWEB开发","老张","一本好书!")); map.put("2",new Book("2","JDBC开发","老张","一本好书!")); map.put("3",new Book("3","Spring开发","老黎","一本好书!")); map.put("4",new Book("4","struts开发","老毕","一本好书!")); map.put("5",new Book("5","android开发","老黎","一本好书!")); } public static Map getAll(){ return map; } } class Book{ private String id; private String name; private String author; private String description; public Book(){ super(); } 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; } }
商品,这个时候,就要找到原来的那个id,删除它,将此次浏览的记录加入到最前面。
即BookHistory="2,5,1"变成了BookHistory="1,2,5"
package cn.edu.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; //显示商品详细信息的Servlet public class CookieDemo4 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); PrintWriter out=response.getWriter(); //根据用户带过来的id,显示相应商品的详细信息 //getParameter是拿到客户机带过来的数据(通过表单和链接传递的参数使用getParameter) String id =request.getParameter("id");//当两个Web组件之间为转发关系时采用getAttribute Book book=(Book) Db.getAll().get(id); out.write(book.getId()+"<br/>"); out.write(book.getAuthor()+"<br/>"); out.write(book.getName()+"<br/>"); out.write(book.getDescription()+"<br/>"); //构建cookie,回写给浏览器 /*一个web应用最多能被浏览器记录20个左右的cookie 所以这里要把大量的cookie记录到一起,记录成一个cookie*/ String CookieValue =buildCookie(id,request); Cookie cookie=new Cookie("BookHistory",CookieValue); cookie.setMaxAge(1*30*24*3600); cookie.setPath("/day07"); response.addCookie(cookie); } private String buildCookie(String id, HttpServletRequest request) { //四种cookie情况 //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 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(); } } if(BookHistory==null){ return id; } //分割成数组,之后将数组转成集合,集合转成链表 LinkedList<String> list=new LinkedList(Arrays.asList(BookHistory.split("\\,"))); //分情况分配新的cookie值 if(list.contains(id)){ //BookHistory=2,5,1 1 1,2,5 list.remove(id); list.addFirst(id); }else{ if(list.size()>=3){ //BookHistory=2,5,4 1 1,2,5 list.removeLast(); list.addFirst(id); }else{ //BookHistory=2,5 1 1,2,5 //BookHistory=null 1 1 list.addFirst(id); } } StringBuffer sb=new StringBuffer(); for(String bid : list){ sb.append(bid+","); } return sb.deleteCharAt(sb.length()-1).toString(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }
至此,介绍了使用cookie记录商品浏览记录的小例子。
转载请注明出处:http://blog.csdn.net/acmman