码迷,mamicode.com
首页 > Web开发 > 详细

网页浏览历史纪录(Cookie技术)

时间:2016-01-01 20:53:29      阅读:469      评论:0      收藏:0      [点我收藏+]

标签:

一、实现原理:
  1. 首先创建一个Cookie用于记录访问网页的编号:或者商品的编号
  2. 每次访问完就往这个Cookie中更新新的数据(新建Cookie重新添加,主要用于访问的页面在原有历史纪录中已经存在,需要删除原有纪录重新添加)
  3. 在需要显示的历史纪录的数据准备页面进行获取,然后转发给显示JSP页面,进行显示。
    技术分享
二、DEMO
图书数据准备页面:ShowBookList (Servlet)
  1. package com.heima.demo1;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.LinkedHashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import javax.servlet.ServletContext;
  8. import javax.servlet.ServletException;
  9. import javax.servlet.http.Cookie;
  10. import javax.servlet.http.HttpServlet;
  11. import javax.servlet.http.HttpServletRequest;
  12. import javax.servlet.http.HttpServletResponse;
  13. import com.heima.beans.Book;
  14. public class ShowBookList extends HttpServlet
  15. {
  16. /**
  17. * Destruction of the servlet. <br>
  18. */
  19. public void destroy()
  20. {
  21. super.destroy(); // Just puts "destroy" string in log
  22. // Put your code here
  23. }
  24. public void doGet(HttpServletRequest request, HttpServletResponse response)
  25. throws ServletException, IOException
  26. {
  27. // 准备图书信息
  28. ServletContext context = this.getServletContext();
  29. Map<String, Book> map = (Map<String, Book>) context.getAttribute("map");
  30. request.setAttribute("map", map);
  31. // 取出历史纪录
  32. List<Book> history = new ArrayList<Book>();
  33. Cookie[] cookies = request.getCookies();
  34. // 判断cookies是否为空,很重要,否者会出现无法获取数据,
  35. // 和空指针异常的错误
  36. if (cookies != null)
  37. {
  38. //遍历cookie数组
  39. for (Cookie c : cookies)
  40. {
  41. //找到带有历史信息的cookie
  42. if ("ids".equals(c.getName()))
  43. {
  44. //获取其中的数据
  45. String ids = c.getValue();
  46. //把原有的数据,切割成数组,取出每个书的编号信息
  47. String[] split = ids.split("-");
  48. for (String s : split)
  49. {
  50. //通过编号信息,取出对应的书
  51. Book b = map.get(s);
  52. // 把所有的书添加到一个list集合中,方便数据的传递
  53. history.add(b);
  54. }
  55. //把数据设置到request域中
  56. request.setAttribute("history", history);
  57. }
  58. }
  59. }
  60. // 把准备好的数据发送到页面进行显示
  61. request.getRequestDispatcher("/showBookList.jsp").forward(request,
  62. response);
  63. }
  64. public void doPost(HttpServletRequest request, HttpServletResponse response)
  65. throws ServletException, IOException
  66. {
  67. doGet(request, response);
  68. }
  69. public void init() throws ServletException
  70. {
  71. //进行图书页面的显示图书的数据准备
  72. Map<String, Book> map = new LinkedHashMap<String, Book>();
  73. for (int i = 0; i < 10; i++)
  74. {
  75. Book b = new Book();
  76. b.setId(i + "");
  77. b.setName("java" + i);
  78. b.setNum(i);
  79. b.setPrice((i + 20) + "");
  80. // System.out.println(i);
  81. map.put(b.getId(), b);
  82. }
  83. //把拥有图书数据的map集合放到ServletContext域中,给所有程序共享
  84. this.getServletContext().setAttribute("map", map);
  85. }
  86. }
图书展示页面:showBookList.jsp
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  8. <title>Insert title here</title>
  9. </head>
  10. <body>
  11. <c:forEach items="${requestScope.map }" var="entry">
  12. <h2><a href="/Test5/showBookInfo?id=${entry.key}">《${entry.value.name }》</a></h2>
  13. </c:forEach>
  14. <h3>您浏览过的图书如下</h3>
  15. <c:forEach items="${requestScope.history}" var="book">
  16. <h2>《${book.name}》</h2>
  17. </c:forEach>
  18. </body>
  19. </html>

图书具体信息准备:ShowBookInfo (Servlet)
  1. package com.heima.demo1;
  2. import java.io.IOException;
  3. import java.util.Arrays;
  4. import java.util.LinkedList;
  5. import java.util.List;
  6. import java.util.Map;
  7. import javax.servlet.ServletContext;
  8. import javax.servlet.ServletException;
  9. import javax.servlet.http.Cookie;
  10. import javax.servlet.http.HttpServlet;
  11. import javax.servlet.http.HttpServletRequest;
  12. import javax.servlet.http.HttpServletResponse;
  13. import com.heima.beans.Book;
  14. public class ShowBookInfo extends HttpServlet
  15. {
  16. /**
  17. * Destruction of the servlet. <br>
  18. */
  19. public void destroy()
  20. {
  21. super.destroy(); // Just puts "destroy" string in log
  22. // Put your code here
  23. }
  24. public void doGet(HttpServletRequest request, HttpServletResponse response)
  25. throws ServletException, IOException
  26. {
  27. // 获取ServletContext域中的map集合
  28. ServletContext context = this.getServletContext();
  29. Map<String, Book> map = (Map<String, Book>) context.getAttribute("map");
  30. // 获取图书编号
  31. String key = request.getParameter("id");
  32. Book b = map.get(key);
  33. // 创建浏览记录
  34. Cookie[] cookies = request.getCookies();
  35. // 用于判断是否是第一次访问
  36. boolean flag = true;
  37. // 判断cookies是否为空,很重要,否者会出现无法获取数据,
  38. // 和空指针异常的错误
  39. if (cookies != null)
  40. {
  41. //遍历数组
  42. for (Cookie cook : cookies)
  43. {
  44. //取出我们需要的历史纪录的数据
  45. if ("ids".equals(cook.getName()))
  46. {
  47. //如果有数据说明不是第一访问,把开关置为false
  48. flag = false;
  49. //获取cookie保存的数据
  50. String ids = cook.getValue();
  51. //按格式切割出我们原来的数据,这里是图书的编号
  52. String[] split = ids.split("-");
  53. //把数组转换为集合方便操作
  54. List<String> asList = Arrays.asList(split);
  55. LinkedList<String> list = new LinkedList(asList);
  56. //如果集合中包含当前访问的页面
  57. if (list.contains(key))
  58. {
  59. //把集合中的历史纪录删除
  60. list.remove(key);
  61. }
  62. //重新添加当前访问页面的纪录,添加在首歌
  63. list.addFirst(key);
  64. //把集合中的数据重新拼接成字符串数据,给Cookie保存
  65. StringBuffer sb = new StringBuffer();
  66. //只需要最大4次的访问记录
  67. for (int i = 0; i < list.size() && i < 4; i++)
  68. {
  69. sb.append(list.get(i));
  70. sb.append("-");
  71. }
  72. //把字符串末尾的“-”去除
  73. ids = sb.substring(0, sb.length() - 1);
  74. // System.out.println(ids);
  75. //重新创建一个名为ids的Cookies
  76. Cookie c = new Cookie("ids", ids);
  77. //向response域添加Cookie,覆盖原有Cookie
  78. response.addCookie(c);
  79. }
  80. }
  81. }
  82. //判断开关,如果为真则是第一次访问
  83. if (flag)
  84. {
  85. //新建名为ids的Cookie,保存当前访问的数据
  86. Cookie c = new Cookie("ids", key);
  87. //添加Cookie
  88. response.addCookie(c);
  89. }
  90. // 获取准备图书信息
  91. request.setAttribute("book", b);
  92. //把准备好的数据发送到显示页面
  93. request.getRequestDispatcher("/showBookInfo.jsp").forward(request,
  94. response);
  95. }
  96. public void doPost(HttpServletRequest request, HttpServletResponse response)
  97. throws ServletException, IOException
  98. {
  99. doGet(request, response);
  100. }
  101. /**
  102. * Initialization of the servlet. <br>
  103. *
  104. * @throws ServletException
  105. * if an error occurs
  106. */
  107. public void init() throws ServletException
  108. {
  109. }
  110. }

图书具体信息显示页:showBookInfo.jsp
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <h1>图书的详细信息</h1>
  11. <h3>图书名称:《${requestScope.book.name }》</h3>
  12. <h3>图书价格:${requestScope.book.price }</h3>
  13. <h3>图书描述:${requestScope.book.description }</h3>
  14. <h2><a href="/Test5/showBookList">返回图书列表</a></h2>
  15. </body>
  16. </html>







 

网页浏览历史纪录(Cookie技术)

标签:

原文地址:http://www.cnblogs.com/didixyy/p/bd979620be4670537d9fa0a42551cdb2.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!