cookie记录浏览记录

HashMap也是我们使用非常多的Collection,它是基于哈希表的 Map 接口的实现,以key-value的形式存在。在HashMap中,key-value总是会当做一个整体来处理,系统会根据hash算法来来计算key-value的存储位置,我们总是可以通过key快速地存、取value。下面就来分析HashMap的存取。

javabean.java

定义Book类的五个属性

  1. package Book.bean;
  2.  
  3. public class Book {
  4.  
  5. private String id;
  6.  
  7. private String bookName;
  8.  
  9. private String author;
  10.  
  11. private float price;
  12.  
  13. private String description;
  14.  
  15. //带参数的构造函数
  16. public Book(String id, String bookName, String author, float price,
  17. String description) {
  18. this.id = id;
  19. this.bookName = bookName;
  20. this.author = author;
  21. this.price = price;
  22. this.description = description;
  23. }
  24.  
  25. public String getId() {
  26. return id;
  27. }
  28.  
  29. public void setId(String id) {
  30. this.id = id;
  31. }
  32.  
  33. public String getBookName() {
  34. return bookName;
  35. }
  36.  
  37. public void setBookName(String bookName) {
  38. this.bookName = bookName;
  39. }
  40.  
  41. public String getAuthor() {
  42. return author;
  43. }
  44.  
  45. public void setAuthor(String author) {
  46. this.author = author;
  47. }
  48.  
  49. public float getPrice() {
  50. return price;
  51. }
  52.  
  53. public void setPrice(float price) {
  54. this.price = price;
  55. }
  56.  
  57. public String getDescription() {
  58. return description;
  59. }
  60.  
  61. public void setDescription(String description) {
  62. this.description = description;
  63. }
  64.  
  65. @Override
  66. public String toString() {
  67. return "Book [id=" + id + ", bookName=" + bookName + ", author="
  68. + author + ", price=" + price + ", description=" + description
  69. + "]";
  70. }
  71. }

BookUtils.java

存取书的内容,利用hashMap来存取数据

  1. package Book.utils;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. import Book.bean.Book;
  7.  
  8. //书放到数据库中的实现类
  9. public class BookUtils {
  10.  
  11. //为了方便,创建一个静态的Map
  12. private static Map<String,Book> map = new HashMap<String,Book>();
  13. //静态块
  14. static{
  15. map.put("", new Book("","降龙十1掌","金庸1",,"武功绝学降龙十1掌"));
  16. map.put("", new Book("","降龙十2掌","金庸2",,"武功绝学降龙十2掌"));
  17. map.put("", new Book("","降龙十3掌","金庸3",,"武功绝学降龙十3掌"));
  18. map.put("", new Book("","降龙十4掌","金庸4",,"武功绝学降龙十4掌"));
  19. map.put("", new Book("","降龙十5掌","金庸5",,"武功绝学降龙十5掌"));
  20. map.put("", new Book("","降龙十6掌","金庸6",,"武功绝学降龙十6掌"));
  21. map.put("", new Book("","降龙十7掌","金庸7",,"武功绝学降龙十7掌"));
  22. map.put("", new Book("","降龙十8掌","金庸8",,"武功绝学降龙十8掌"));
  23. map.put("", new Book("","降龙十9掌","金庸9",,"武功绝学降龙十9掌"));
  24. map.put("", new Book("","降龙十掌","金庸10",,"武功绝学降龙十掌"));
  25. map.put("", new Book("","降龙十1掌","金庸11",,"武功绝学降龙十1掌"));
  26. }
  27. //拿取书
  28. public static Map<String,Book> getAllBook(){
  29. return map;
  30. }
  31. //获取一本书
  32. public static Book getBookById(String id){
  33. return map.get(id);
  34. }
  35. }

showAllBook.java

读取并显示书籍信息

  1. package Book.servlet;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.util.Map;
  6.  
  7. import javax.servlet.ServletException;
  8. import javax.servlet.http.Cookie;
  9. import javax.servlet.http.HttpServlet;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;
  12.  
  13. import Book.bean.Book;
  14. import Book.utils.BookUtils;
  15.  
  16. public class showAllBook extends HttpServlet {
  17.  
  18. /**
  19. * 1.显示所有的书
  20. * 2.显示浏览的历史记录
  21. * The doGet method of the servlet. <br>
  22. *
  23. * This method is called when a form has its tag value method equals to get.
  24. *
  25. * @param request the request send by the client to the server
  26. * @param response the response send by the server to the client
  27. * @throws ServletException if an error occurred
  28. * @throws IOException if an error occurred
  29. */
  30. public void doGet(HttpServletRequest request, HttpServletResponse response)
  31. throws ServletException, IOException {
  32.  
  33. request.setCharacterEncoding("utf-8");
  34. response.setContentType("text/html;charset=UTF-8");
  35. PrintWriter out = response.getWriter();
  36.  
  37. out.write("书架:<br>");
  38. //1.显示所有的书
  39. Map<String,Book> map = BookUtils.getAllBook();
  40. //遍历集合 foreach
  41. for (Map.Entry<String, Book> entry : map.entrySet()) {
  42. //拿到每一本书的id
  43. String id = entry.getKey();
  44. //拿到每一本书
  45. Book book = entry.getValue();
  46. //output book name
  47. //客户端跳转
  48. out.write(book.getBookName() + "&nbsp;<a href='" + request.getContextPath() + "/servlet/ShowDetail?id=" + id + " '>显示详细信息</a><br>");
  49.  
  50. }
  51.  
  52. out.write("<br><br><br>");
  53. //显示浏览的历史记录:cookie的名字定义为history : 值的形式:1-2-3
  54. //拿到cookie,
  55. Cookie[] cs = request.getCookies();
  56. //for
  57. for (int i = ; cs != null && i < cs.length; i++) {
  58. Cookie c = cs[i];
  59. if("history".equals(c.getName())){
  60. //show
  61. out.write("你的浏览记录:<br>");
  62. //got cookie
  63. String value = c.getValue();
  64. //根据形式来拆分成数组
  65. String [] ids = value.split("-");
  66. //show the book
  67. for (int j = ; j < ids.length; j++) {
  68. Book b = BookUtils.getBookById(ids[j]);
  69. out.write(b.getBookName() + "<br>");
  70. }
  71. }
  72. }
  73.  
  74. }
  75.  
  76. /**
  77. * The doPost method of the servlet. <br>
  78. *
  79. * This method is called when a form has its tag value method equals to post.
  80. *
  81. * @param request the request send by the client to the server
  82. * @param response the response send by the server to the client
  83. * @throws ServletException if an error occurred
  84. * @throws IOException if an error occurred
  85. */
  86. public void doPost(HttpServletRequest request, HttpServletResponse response)
  87. throws ServletException, IOException {
  88.  
  89. doGet(request, response);
  90. }
  91.  
  92. }

ShowDetail.java

1.显示书的详细信息: 获取传递过来的 id ,通过BookUtils来获取书的全部信息

2.显示浏览记录 : 获取传递过来的cookie,分析处理cookie

  1. package Book.servlet;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.util.Arrays;
  6. import java.util.LinkedList;
  7.  
  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.  
  14. import Book.bean.Book;
  15. import Book.utils.BookUtils;
  16.  
  17. /**
  18. * 1.显示书的详细信息
  19. * 2.发送历史浏览记录
  20. * @author kj
  21. *
  22. */
  23. public class ShowDetail extends HttpServlet {
  24.  
  25. /**
  26. * The doGet method of the servlet. <br>
  27. *
  28. * This method is called when a form has its tag value method equals to get.
  29. *
  30. * @param request the request send by the client to the server
  31. * @param response the response send by the server to the client
  32. * @throws ServletException if an error occurred
  33. * @throws IOException if an error occurred
  34. */
  35. public void doGet(HttpServletRequest request, HttpServletResponse response)
  36. throws ServletException, IOException {
  37.  
  38. request.setCharacterEncoding("utf-8");
  39. response.setContentType("text/html;charset=UTF-8");
  40. PrintWriter out = response.getWriter();
  41. //1.拿到传递的数据
  42. String id = request.getParameter("id");
  43. //根据id查询书
  44. Book book = BookUtils.getBookById(id);
  45. //显示书的信息
  46. out.write(book + "&nbsp;&nbsp;<a href='" + request.getContextPath() + "/servlet/showAllBook'>返回主页继续浏览</a><br>");
  47.  
  48. //2.发送cookie
  49. //获取历史记录字符串
  50. String history = getHistory(request,id);
  51. //创建cookie
  52. Cookie c = new Cookie("history",history);
  53. c.setMaxAge(Integer.MAX_VALUE);
  54. //设置Cookie存放路径
  55. c.setPath(request.getContextPath());
  56. response.addCookie(c);
  57. }
  58.  
  59. /**
  60. * 获取要发往客户端的历史记录的字符串
  61. * @return
  62. */
  63. private String getHistory(HttpServletRequest request, String id) {
  64. // 获取字符串的情况
  65. /**
  66. * 历史记录的的cookie被获取 点击的书 结果
  67. * 1. null n n
  68. * 2. 1 1 1
  69. * 3. 1 2 2-1
  70. * 4 1-2 1 1-2
  71. * 5. 1-2 2 2-1
  72. * 6. 1-2 3 3-1-2
  73. * 7. 1-2-3 1 1-2-3
  74. * 8. 1-2-3 2 2-1-3
  75. * 9. 1-2-3 3 3-1-2
  76. * 10 1-2-3 4 4-1-2
  77. */
  78. //设定一个cookie为空,用来存放获取到的原来的cookie
  79. Cookie history = null;
  80. //拿到所有的cookie
  81. Cookie[] cs = request.getCookies();
  82. //循环判断所有的cookie
  83. for (int i = ; cs!=null && i < cs.length; i++) {
  84. if(cs[i].getName().equals("history")){
  85. history = cs[i];
  86. break;
  87. }
  88. }
  89. //情况1,history为空,没有,就把id添加进去
  90. if(history == null)
  91. return id;
  92. //如果不为空
  93. String value = history.getValue();
  94. System.out.println("----value的长度-----" + value.length()+"***value的值***"+ value);
  95. if(value.length() == ){
  96. //2,3的情况
  97. if(value.equals(id)){
  98. //第一种情况
  99. return id;
  100. }else{
  101. return id + "-" + value;
  102. }
  103.  
  104. }
  105. //剩下的就是大于1的情况了,说明有两个或两个以上的,这就需要拆封成单个字符串了
  106. String[] ids = value.split("-");
  107. ////Arrays.asList 返回一个受指定数组支持的固定大小的列表,也可以用for循环
  108. LinkedList<String> list = new LinkedList<String>(Arrays.asList(ids));
  109. //返回此列表中首次出现的指定元素的索引,如果此列表 中不包含该元素,则返回 -1
  110. int index = list.indexOf(id);
  111. //4,5,6的情况
  112.  
  113. // value的值包括 “a” “-” “b” 所以是3
  114. if(value.length() == ){
  115. System.out.println("######进入 value=3 的情况######");
  116. if(index == -){
  117. //说明没有点击过
  118. list.addFirst(id);
  119. }else{
  120. //说明是点击过的书
  121. list.remove(index);
  122. list.add(id);
  123. }
  124. }
  125. //7,8,9,10的情况,都是三个数
  126. if(value.length() > ){
  127. System.out.println("@@@@@@@进入 value>3 的情况@@@@@@@");
  128. if(index == - ){
  129. list.removeLast();
  130. list.addFirst(id);
  131. }else{
  132. list.remove(index);
  133. list.addFirst(id);
  134. }
  135. }
  136.  
  137. //处理完成后需要将数据输出成字符串的形式
  138. StringBuffer sb = new StringBuffer(list.get());
  139. for (int j = ; j < list.size(); j++) {
  140. sb.append("-" + list.get(j));
  141. }
  142.  
  143. return sb.toString();
  144. }
  145.  
  146. public void doPost(HttpServletRequest request, HttpServletResponse response)
  147. throws ServletException, IOException {
  148.  
  149. request.setCharacterEncoding("utf-8");
  150. response.setContentType("text/html;charset=UTF-8");
  151. PrintWriter out = response.getWriter();
  152. doGet(request, response);
  153. }
  154. }

cookie记录浏览记录的更多相关文章

  1. 简单的Cookie记录浏览记录案例

    books.jsp 界面 代码 <%@ page contentType="text/html;charset=UTF-8" language="java" ...

  2. Cookie 简单使用记录浏览记录

    ItemsDAO.java package dao; import java.util.* ; import java.sql.* ; import util.DBHelper; import ent ...

  3. jquery.cookie.js结合asp.net实现最近浏览记录

    一.html代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...

  4. Cookie实现商品浏览记录--方式二:JS实现

    使用Cookie实现商品浏览记录:方式二:JS方法实现cookie的获取以及写入.当某一个产品被点击时,触发JS方法.利用JS方法判断一下,此产品是否在浏览记录中.如果不存在,则将产品ID加入到coo ...

  5. 使用cookie实现打印浏览记录的功能

    可以用cookie知识来实现打印浏览记录.这里面用到的思路是将浏览记录以字符串的方式保存到cookie中,当浏览记录增加时,再将其转化为数组. $uri=$_SERVER['REQUEST_URI'] ...

  6. (JS实现顾客商品浏览记录以及购物车)Cookie的保存与删除

    //JS实现顾客浏览商品的记录以及实现购物车的功能function setCookie(name,value) { var Days = 30; var exp = new Date(); exp.s ...

  7. 使用Cookie保存商品浏览记录

    数据流程:页面上是商品列表,点击<a href="productServlet">商品名</a> ==>跳转到自定义的servlet中进行处理,先得到 ...

  8. Cookie中图片的浏览记录与cookie读取servle时路径的设置(文字描述)

    public class ShowServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpSer ...

  9. js操作Cookie,实现历史浏览记录

    /** * history_teacher.jsp中的js,最近浏览名师 * @version: 1.0 * @author: mingming */ $(function(){ getHistory ...

随机推荐

  1. java.lang.UnsupportedClassVersionError(java项目版本一致问题)

    报此错误,一般都是由于在myeclipse中的java项目是用高版本(jdk1.6之后)的jdk进行编译后生成的class文件,却要运行在低版本的jdk虚拟机上,导致这个错误 解决办法: 在myecl ...

  2. dom4j中 selectSingleNode 或selectNodes获取不到节点的原因总结 (转)

    没想到搞个dom4j会出这么多怪错.. 最近在研究XBRL GL的有关内容,在项目中要求吧XBRL GL导入到11179注册库中,根据11179建立数据库,然后从XBRL GL分类标准中导入数据到数据 ...

  3. bzoj1597

    首先不难想到排序,这种无规律的东西一般都要转化为有规律才好做 首先以x为第一关键字,y为第二关键字升序排序 拍完序我们发现,若存在两块土地i,j x[i]<=x[j],y[i]<=y[j] ...

  4. [FJSC2014]圈地

    [题目描述] 2维平面上有n个木桩,黄学长有一次圈地的机会并得到圈到的土地,为了体现他的高风亮节,他要使他圈到的土地面积尽量小.圈地需要圈一个至少3个点的多边形,多边形的顶点就是一个木桩,圈得的土地就 ...

  5. ArcServer,ArcSDE,ArcIMS,ArcEngine

    ArcServer,ArcSDE,ArcIMS,ArcEngine是ESRI的四种产品ArcGIS Server 与 ArcIMS功能相似,是将地图发布成服务供调用的ArcSDE 是空间数据引擎,是将 ...

  6. AjaxPro使用说明

    转自:http://www.cnblogs.com/lexus/archive/2007/11/29/977281.html 目录 AjaxPro使用说明    1 目录    2 修改历史纪录    ...

  7. Devexpress 之gridControl

    1.gridControl如何去掉主面板? 鼠标右键Run Designer=>OptionsView => ShowGroupPanel=False: 2.gridControl如何设置 ...

  8. 《C语言程序设计现代方法》第1章 C语言概述

    C语言的特点:C语言是一种底层语言.C语言是一种小型语言.C语言是一种包容性语言. C语言的优点:高效.可移植.功能强大.灵活.标准库.与UNIX系统集成. C语言的缺点:C程序更容易隐藏错误.C程序 ...

  9. 传输层之UDP

    1.UDP的定义 跟tcp一样,我们把她定义为: 无连接的,不可靠的,用户数据报协议. 从中我们看到了:无连接和不可靠,这是它的缺点也是它的优点,因为他选择了性能,舍弃了部分安全,节约资源,速度快. ...

  10. 2D游戏编程3—GDI

    WM_PAINT消息触发程序重新绘制界面,过程如下: PAINTSTRUCT    ps;    // used in WM_PAINT HDC        hdc;    // handle to ...