package com.cn.cookie; import java.io.IOException; 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; public class CookieDemo extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置乱码问题 response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); // 输出网站所有商品 response.getWriter().write("本网站有商品如下:<br/>"); Map<String, Book> map = DB.getAll(); for (Map.Entry<String, Book> entry : map.entrySet()) { Book book = entry.getValue(); response.getWriter() .write("<a href =‘/Day1/servlet/CookieDemo2?id=" + book.getId() + "‘target=_blank‘>" + book.getName() + "</a><br/>"); } response.getWriter().write("<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) { Book book = (Book) DB.getAll().get(id); response.getWriter().write(book.getName() + "<br/>"); } } } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } // 商品的数据库 class DB { private static Map<String, Book> map = new LinkedHashMap<String, Book>(); static { map.put("1", new Book("1", "javaweb开发", "老张", "一本好书")); map.put("2", new Book("2", "jdbc", "老林", "一本好书")); map.put("3", new Book("3", "java", "老萧", "一本好书")); map.put("4", new Book("4", "tomcat", "老哥", "一本好书")); map.put("5", new Book("5", "mysql", "老李", "一本好书")); } public static Map getAll() { return map; } } class Book { private String id; private String name; private String author; private String desc; public Book() { super(); // TODO Auto-generated constructor stub } public Book(String id, String name, String author, String desc) { super(); this.id = id; this.name = name; this.author = author; this.desc = desc; } 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 getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } }
package com.cn.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 CookieDemo2 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置中文乱码问题 response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); PrintWriter pw = response.getWriter(); // 根据用户带过来的id,显示对应商品的详细信息。 String id = request.getParameter("id"); Book book = (Book) DB.getAll().get(id); pw.write(book.getId() + "<br/>"); pw.write(book.getName() + "<br/>"); pw.write(book.getAuthor() + "<br/>"); pw.write(book.getDesc() + "<br/>"); // 构建cookie,回写给浏览器 String cookieValue = buildCookie(id, request); Cookie cookie = new Cookie("BookHistory", cookieValue); cookie.setMaxAge(1 * 60 * 60 * 24 * 30); cookie.setPath("/Day1"); response.addCookie(cookie); } private String buildCookie(String id, HttpServletRequest request) { 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; } /* * asList():数组转成集合 由于list方法查询慢,所以用linkedList */ LinkedList<String> list = new LinkedList<String>( Arrays.asList(BookHistory.split("\\,"))); // 由于是按照查看商品的最近记录进行显示的,所以如果以前有浏览过,现在又重新浏览,那么把以前的删掉,然后把现在浏览的放在前面 if (list.contains(id)) { list.remove(id); // 因为最多只能显示3个商品,所以把以前浏览过的最后一个商品删除 } else { if (list.size() >= 3) { list.removeLast(); } } list.addFirst(id); StringBuffer sb = new StringBuffer(); for (String id2 : list) { sb.append(id2 + ","); } return sb.deleteCharAt(sb.length() - 1).toString(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
时间: 2024-10-10 00:54:35