javaWeb 使用cookie显示商品浏览记录
package de.bvb.cookie; import java.io.IOException; import java.io.PrintWriter; import java.util.Date; import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 显示商品列表和已经浏览过的商品 * * @author joker * */ public class CookieDemo11 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(); // 显示商品列表 out.write("商品列表: <br/>"); for (Entry<String, Book> b : Db.getBooks().entrySet()) { out.write("<a target='_blank' href='/web/CookieDemo12?id=" + b.getKey() + "'>" + b.getValue().getName() + "</a><br/>"); } // 显示浏览过的商品 out.write(" <br/> <br/>浏览过的商品: <br/>"); Cookie[] cookies = request.getCookies(); for (int i = 0; cookies != null && i < cookies.length; i++) { if (cookies[i].getName().equals("history")) { String[] ids = cookies[i].getValue().split("\\,"); for (String s : ids) { out.write(Db.getBooks().get(s).getName() + "<br/>"); } } } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } class Db { private static LinkedHashMap<String, Book> books = new LinkedHashMap<String, Book>(); static { books.put("1", new Book("1", "javaWeb", "javaweb")); books.put("2", new Book("2", "android", "android")); books.put("3", new Book("3", "spring", "spring")); books.put("4", new Book("4", "strus", "strus")); books.put("5", new Book("5", "ios", "ios")); } public static Map<String, Book> getBooks() { return books; } } class Book { private String id; private String name; private String description; public Book(String id, String name, String description) { super(); this.id = id; this.name = name; this.description = description; } public Book() { super(); } 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 getDescription() { return description; } public void setDescription(String description) { this.description = description; } }
package de.bvb.cookie; import java.io.IOException; import java.io.PrintWriter; import java.util.Arrays; import java.util.Date; import java.util.LinkedList; import java.util.Map.Entry; import javax.enterprise.inject.ResolutionException; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.swing.text.DateFormatter; /** * 显示商品详情 * * @author joker * */ public class CookieDemo12 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(); // 显示商品详情 out.write("商品详情: <br/>"); String id = request.getParameter("id"); for (Entry<String, Book> b : Db.getBooks().entrySet()) { if (id.equals(b.getKey())) { out.write(b.getValue().getId() + "<br/>"); out.write(b.getValue().getName() + "<br/>"); out.write(b.getValue().getDescription() + "<br/>"); } } // 回写cookie,保存最后浏览的商品id String history = buildIds(id, request); Cookie cookie = new Cookie("history", history); cookie.setMaxAge(1 * 30 * 24 * 60 * 60); cookie.setPath("/web"); response.addCookie(cookie); } private String buildIds(String id, HttpServletRequest request) { String history = null; Cookie[] cookies = request.getCookies(); for (int i = 0; cookies != null && i < cookies.length; i++) { if (cookies[i].getName().equals("history")) { history = cookies[i].getValue(); } } if (history == null) { return id; } LinkedList<String> ids = new LinkedList<String>(Arrays.asList(history .split("\\,"))); if (ids.contains(id)) { ids.remove(id); } else { if (ids.size() >= 3) { // 最多显示3条浏览历史 ids.removeLast(); } } ids.addFirst(id); StringBuffer sb = new StringBuffer(); for (String s : ids) { sb.append(s).append(","); } return sb.deleteCharAt(sb.length() - 1).toString(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }