Cookie案例-显示商品流量历史记录
2014-11-12 10:16
507 查看
package com; 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; public class test1 extends HttpServlet { protected 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/>"); Map<String, Book> map = Db.getAll(); for (Map.Entry<String, Book> entry : map.entrySet()) { Book book = entry.getValue(); out.print("<a href='test2?id=" + book.getId() + "' target='_blank' >" + book.getName() + "</a><br/>"); } //显示用户曾经看过的商品 out.print("<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); out.print(book.getName() + "<br/>"); } } } } } //Db作为数据库 class Db { private static Map<String, Book> map = new LinkedHashMap<String, Book>(); static { map.put("1", new Book("1", "Java WEB开发", "WY", "好书")); map.put("2", new Book("2", "WEB开发", "zt", "一般")); map.put("3", new Book("3", "程序设计", "df", "较好书")); map.put("4", new Book("4", "计算机组成", "as", "一般好书")); map.put("5", new Book("5", "编译原理", "ty", "很好书")); map.put("6", new Book("6", "网络维护", "hj", "非常好书")); } public static Map<String, Book> 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; } }
package com; 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 test2 extends HttpServlet { private static final long serialVersionUID = 1L; protected 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 ,显示响应商品的详细信息 String id = request.getParameter("id"); Book book = (Book) Db.getAll().get(id); out.write(book.getId() + "<br/>"); out.write(book.getAuthor() + "<br/>"); out.write(book.getDescription() + "<br/>"); out.write(book.getName() + "<br/>"); //构建Cookie ,回写给浏览器 String cookieValue = buildCookie(id, request); Cookie cookie = new Cookie("bookHistory", cookieValue); cookie.setMaxAge(30 * 24 * 3600); cookie.setPath("/Cookie"); response.addCookie(cookie); } private String buildCookie(String id, HttpServletRequest request) { //如果用户没有带任何的Cookie过来 //如果用户带了Cookie来了包含在看过的id中,则要把id删掉 //带了Cookie过来了但是没有包含看过的id //带了Cookie过来但是没有包含 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<String>(Arrays.asList(bookHistory.split("\\,"))); if (list.contains(id)) { list.remove(id); } else if (list.size() >= 3) { list.removeLast(); } list.addFirst(id); StringBuffer sb = new StringBuffer(); for (String bid : list) { sb.append(bid).append(","); } return sb.deleteCharAt(sb.length() - 1).toString(); } }
相关文章推荐
- 利用Cookie显示商品浏览历史记录
- Cookie案例-显示商品浏览历史纪录
- javaWeb_04-Cookie案例-显示商品浏览历史纪录
- javaWeb_04-Cookie案例-显示商品浏览历史纪录
- Cookie案例-显示商品浏览历史纪录
- Cookie案例-显示商品浏览历史纪录
- cookie案例:显示商品浏览历史
- JavaWeb的Cookie技术显示历史商品( 信息 )浏览记录(学习方立勋老师的教程)
- 【Cookie:默认存储在浏览器的内存中】显示上一次访问时间、商品浏览记录
- Cookie技术案例-——显示曾经浏览过的商品
- javaweb之Cookie显示商品的浏览记录和Cookie的常见应用有哪些
- Servlet—Cookie(显示用户上次访问时间、显示商品浏览历史)
- servlet显示商品浏览历史记录
- java(16)--利用cookie显示商品浏览历史
- Session案例,显示商品浏览记录
- 利用cookie显示商品的浏览记录
- 4000 .会话管理之Cookie案例二:显示已浏览商品
- 自己写的一个php利用cookie记录浏览(商品)历史记录的函数
- cookie案例之显示用户上次浏览过的商品
- Java Cookie记录商品浏览历史