servlet显示商品浏览历史记录
2014-09-14 18:15
441 查看
假设有一个数据库和书本对象
第一个servlet显示商品列表和历史记录
第二个servlet显示商品具体信息,存储cookie
//数据库 class Db{ private static Map<String, Book> map = new LinkedHashMap(); static{ map.put("1", new Book("1","web开发","老张")); map.put("2", new Book("2","spring","老毕")); map.put("3", new Book("3","struts","老张")); map.put("4", new Book("4","jdbc","老黎")); } //拿到书 public static Map getAll(){ return map; } } //书对象 class Book{ private String id; private String name; private String author; public Book() { super(); } public Book(String id, String name, String author) { super(); this.id = id; this.name = name; this.author = author; } 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; } }
第一个servlet显示商品列表和历史记录
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='/day07/CookieDemo3?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("\\,"); //2,2,3 for(String id : ids){ Book book = (Book) Db.getAll().get(id); out.print(book.getName()+"<br>"); } } } }
第二个servlet显示商品具体信息,存储cookie
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.print(book.getId()+"<br>"); out.print(book.getName()+"<br>"); out.print(book.getAuthor()+"<br>"); //构建Cookie回写给浏览器 String cookieValue = buildCookie(id,request); Cookie cookie = new Cookie("bookHistory", cookieValue); cookie.setPath("/day07"); cookie.setMaxAge(3600*24); 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(); } } /*分情况谈论 * old id now * null 1 1 * 2,3 1 1,2,3 * 1 1 1 * ... */ if(bookHistory==null){ return id; } //LinkedList便于增删改查 LinkedList list = new LinkedList(Arrays.asList(bookHistory.split("\\,"))); if(list.contains(id)){ list.remove(id); list.addFirst(id); }else{ if(list.size()>=3){ list.removeLast(); list.addFirst(id); }else{ list.addFirst(id); } } StringBuilder sb = new StringBuilder(); for(Object ob : list){ sb.append(ob+","); } return sb.deleteCharAt(sb.length()-1).toString(); }
相关文章推荐
- java_Cookies_1_商品浏览历史记录servlet2
- 利用Cookie显示商品浏览历史记录
- Servlet—Cookie(显示用户上次访问时间、显示商品浏览历史)
- java_Cookies_1_商品浏览历史记录servlet1
- JavaWeb的Cookie技术显示历史商品( 信息 )浏览记录(学习方立勋老师的教程)
- 显示商品历史浏览记录
- Servlet—Cookie(显示用户上次访问时间、显示商品浏览历史)
- 利用cookie显示商品的浏览记录
- Java Cookie记录商品浏览历史
- javaWeb 使用cookie显示商品浏览记录
- javaWeb_04-Cookie案例-显示商品浏览历史纪录
- 关于用户浏览商品的历史记录(cookie)
- servlet之利用Cookie显示最近浏览的商品
- Sevlet模拟显示商品的浏览历史
- JavaWeb_02_cookie_显示商品浏览记录
- win8系统如何让任务栏不显示最近打开项目(浏览的历史记录)
- java(16)--利用cookie显示商品浏览历史
- Cookie案例-显示商品浏览历史纪录
- 用$.cookie()来存历史浏览记录,并且显示出来
- javaweb之Cookie显示商品的浏览记录和Cookie的常见应用有哪些