实体类

  1. package entity;
  2.  
  3. public class Product {
  4.  
  5. private String id;
  6. private String proName;
  7. private String proType;
  8. private double price;
  9. public String getId() {
  10. return id;
  11. }
  12. public void setId(String id) {
  13. this.id = id;
  14. }
  15. public String getProName() {
  16. return proName;
  17. }
  18. public void setProName(String proName) {
  19. this.proName = proName;
  20. }
  21. public String getProType() {
  22. return proType;
  23. }
  24. public void setProType(String proType) {
  25. this.proType = proType;
  26. }
  27. public double getPrice() {
  28. return price;
  29. }
  30. public void setPrice(double price) {
  31. this.price = price;
  32. }
  33. public Product(String id, String proName, String proType, double price) {
  34. super();
  35. this.id = id;
  36. this.proName = proName;
  37. this.proType = proType;
  38. this.price = price;
  39. }
  40. public Product() {
  41. super();
  42. // TODO Auto-generated constructor stub
  43. }
  44. @Override
  45. public String toString() {
  46. return "Product [id=" + id + ", price=" + price + ", proName="
  47. + proName + ", proType=" + proType + "]";
  48. }
  49.  
  50. }

商品列表servlet

  1. package servlet;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.util.List;
  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 dao.ProductDao;
  14. import entity.Product;
  15. /*
  16. * 查询所有商品的servlet
  17. */
  18. public class ListServlet extends HttpServlet {
  19.  
  20. public void doGet(HttpServletRequest request, HttpServletResponse response)
  21. throws ServletException, IOException {
  22.  
  23. response.setContentType("text/html; charset=utf-8");
  24.  
  25. ProductDao dao = new ProductDao();
  26. List<Product> list = dao.findAll();
  27.  
  28. //2.把商品显示到浏览器
  29. PrintWriter writer = response.getWriter();
  30. String html = "";
  31.  
  32. html += "<html>";
  33. html += "<head>";
  34. html += "<title>显示商品列表</title>";
  35. html += "</head>";
  36. html += "<body>";
  37. html += "<table border='1' align='center' width='600px'>";
  38. html += "<tr>";
  39. html += "<th>编号</th><th>商品名称</th><th>商品型号</th><th>商品价格</th>";
  40. html += "</tr>";
  41. //遍历商品
  42. if (list != null) {
  43. for(Product p : list) {
  44. html += "<tr>";
  45. html += "<td>"+p.getId()+"</td>" +
  46. "<td><a href='"+request.getContextPath()+"/DetailServlet?id="+p.getId()+"'>"
  47. +p.getProName()+"</a></td><td>"+p.getProType()+"</td><td>"+p.getPrice()+"</td>";
  48. html += "<tr>";
  49. }
  50. }
  51. html += "</table>";
  52.  
  53. /**
  54. * 显示浏览过的商品
  55. */
  56. html += "最近浏览过的商品:<br/>";
  57. Cookie[] cookies = request.getCookies();// 获取cookie对象
  58. if (cookies != null) {
  59. for(Cookie cookie : cookies) {
  60. if (cookie.getName().equals("prodHist")) {
  61. String prodHist = cookie.getValue();
  62. String[] ids = prodHist.split(",");
  63. for (String id : ids) {
  64. //查询数据库,查询对应的商品
  65. Product p = dao.findById(id);
  66. //显示到浏览器
  67. html += "" + p.getId() + "&nbsp;" + p.getProName() + "&nbsp;" + p.getPrice() + "<br/>";
  68. }
  69. }
  70. }
  71. }
  72. html += "</body>";
  73. html += "</html>";
  74.  
  75. writer.write(html);
  76. }
  77.  
  78. public void doPost(HttpServletRequest request, HttpServletResponse response)
  79. throws ServletException, IOException {
  80. doGet(request, response);
  81. }
  82.  
  83. }

商品详情

  1. package servlet;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.util.Arrays;
  6. import java.util.Collection;
  7. import java.util.LinkedList;
  8.  
  9. import javax.servlet.ServletException;
  10. import javax.servlet.http.Cookie;
  11. import javax.servlet.http.HttpServlet;
  12. import javax.servlet.http.HttpServletRequest;
  13. import javax.servlet.http.HttpServletResponse;
  14.  
  15. import dao.ProductDao;
  16. import entity.Product;
  17. /**
  18. * 显示商品详细
  19. * @author Administrator
  20. *
  21. */
  22. public class DetailServlet extends HttpServlet {
  23.  
  24. private static final long serialVersionUID = 1L;
  25.  
  26. public void doGet(HttpServletRequest request, HttpServletResponse response)
  27. throws ServletException, IOException {
  28.  
  29. response.setContentType("text/html; charset=utf-8");
  30.  
  31. //1.获取编号
  32. String id = request.getParameter("id");
  33.  
  34. //2.到数据库中查询对应编号的商品
  35. ProductDao dao = new ProductDao();
  36. Product product = dao.findById(id);
  37.  
  38. //3.显示到浏览器
  39. PrintWriter writer = response.getWriter();
  40. String html = "";
  41.  
  42. html += "<html>";
  43. html += "<head>";
  44. html += "<title>显示商品详细</title>";
  45. html += "</head>";
  46. html += "<body>";
  47. html += "<table border='1' align='center' width='300px'>";
  48. if(product!=null){
  49. html += "<tr><th>编号:</th><td>"+product.getId()+"</td></tr>";
  50. html += "<tr><th>商品名称:</th><td>"+product.getProName()+"</td></tr>";
  51. html += "<tr><th>商品型号:</th><td>"+product.getProType()+"</td></tr>";
  52. html += "<tr><th>商品价格:</th><td>"+product.getPrice()+"</td></tr>";
  53. }
  54.  
  55. html += "</table>";
  56. html += "<center><a href='"+request.getContextPath()+"/ListServlet'>[返回列表]</a></center>";
  57. html += "</body>";
  58. html += "</html>";
  59.  
  60. writer.write(html);
  61.  
  62. /**
  63. * 创建cookie,并发送
  64. */
  65. //1.创建cookie
  66. Cookie cookie = new Cookie("prodHist", createValue(request, id));
  67. cookie.setMaxAge(1*60*60);//一个月
  68. //2.发送cookie
  69. response.addCookie(cookie);
  70. }
  71.  
  72. /*
  73. * 生成cookie的值
  74. * 分析:
  75. * 当前cookie值 传入商品id 最终cookie值
  76. * null或没有prodHist 1 1 (算法: 直接返回传入的id )
  77. * 1 2 2,1 (没有重复且小于3个。算法:直接把传入的id放最前面 )
  78. * 2,1 1 1,2(有重复且小于3个。算法:去除重复id,把传入的id放最前面 )
  79. * 3,2,1 2 2,3,1(有重复且3个。算法:去除重复id,把传入的id放最前面)
  80. * 3,2,1 4 4,3,2(没有重复且3个。算法:去最后的id,把传入的id放最前面)
  81. * @return
  82. */
  83. public String createValue(HttpServletRequest request, String id) {
  84. Cookie[] cookies = request.getCookies();
  85. String prodhist = null;
  86.  
  87. if (cookies != null) {
  88. for(Cookie cookie : cookies) {
  89. if(cookie.getName().equals("prodhist")) {
  90. prodhist = cookie.getValue();
  91. break;
  92. }
  93. }
  94. }
  95.  
  96. // null或没有prodHist
  97. if (cookies == null || prodhist == null) {
  98. // 直接返回传入的id
  99. return id;
  100. }
  101.  
  102. // 3,21 2
  103. //String -> String[] -> Collection :为了方便判断重复id
  104. String[] ids = prodhist.split(",");
  105. Collection<String> colls = Arrays.asList(ids);
  106.  
  107. // LinkedList 方便地操作(增删改元素)集合
  108. // Collection -> LinkedList
  109. LinkedList<String> list = new LinkedList<String>(colls);
  110.  
  111. //不超过3个
  112. if(list.size() < 3) {
  113. if(list.contains(id)) {
  114. //去除重复id,把传入的id放最前面
  115. list.remove(id);
  116. list.addFirst(id);
  117. } else {
  118. //直接把传入的id放最前面
  119. list.addFirst(id);
  120. }
  121. } else {
  122. //等于3个
  123. //id重复
  124. if(list.contains(id)){
  125. //去除重复id,把传入的id放最前面
  126. list.remove(id);
  127. list.addFirst(id);
  128. }else{
  129. //去最后的id,把传入的id放最前面
  130. list.removeLast();
  131. list.addFirst(id);
  132. }
  133. }
  134.  
  135. // LinedList -> String
  136. StringBuffer sb = new StringBuffer();
  137. for (Object obj : list) {
  138. sb.append(obj + ",");
  139. }
  140. //去掉最后的逗号
  141. String result = sb.toString();
  142. result = result.substring(0, result.length()-1);
  143.  
  144. return result;
  145. }
  146.  
  147. }

dao

  1. package dao;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import entity.Product;
  7.  
  8. public class ProductDao {
  9.  
  10. //模拟"数据库",存放所有商品数据
  11. private static List<Product> data = new ArrayList<Product>();
  12.  
  13. /**
  14. * 初始化商品数据
  15. */
  16. static {
  17. //只执行一次
  18. for(int i=1; i<=10; i++) {
  19. data.add(new Product("" + i, "笔记本" + i , "LN00" + i,34.0 + i ));
  20. }
  21. }
  22.  
  23. /**
  24. * 提供查询所有商品的方法
  25. */
  26. public List<Product> findAll() {
  27. return data;
  28. }
  29.  
  30. /**
  31. * 提供根据编号查询商品的方法
  32. */
  33. public Product findById(String id) {
  34. for (Product p : data) {
  35. if(p.getId().equals(id)) {
  36. return p;
  37. }
  38. }
  39. return null;
  40. }
  41. }

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

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

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

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

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

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

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

  4. destoon系统开发-最新利用浏览器的cookie 做历史浏览记录

      注意: 代码 放在要显示的为 (一般放在详情页),注意本教程不入库,直接利用浏览器的 cookie 缓存判断    <!--历史浏览记录 S--> <div class=&quo ...

  5. Cookie实现商品浏览记录--方式一:Java实现

    方式一:Java代码方式实现:此种方式实现思路较为顺畅.难点在于,如何实现将最近浏览的产品显示在最前面:实现方式是借助LinkedList提供的remove()方法,先将此id从列表中移除,然后再借助 ...

  6. javaWeb 使用cookie显示商品浏览记录

    package de.bvb.cookie; import java.io.IOException; import java.io.PrintWriter; import java.util.Date ...

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

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

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

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

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

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

随机推荐

  1. Mysql中错误日志、binlog日志、查询日志、慢查询日志简单介绍

    前言 数据库的日志是帮助数据库管理员,追踪分析数据库以前发生的各种事件的有力根据.mysql中提供了错误日志.binlog日志(二进制日志).查处日志.慢查询日志.在此,我力求解决下面问题:各个日志的 ...

  2. android的ListView点击item使item展开的做法

    直接上代码把.主要是又一次给item measure高度,直接上代码把 import java.util.ArrayList; import android.app.Activity; import ...

  3. iOS音频播放 (二):AudioSession 转

    原文出处 :http://msching.github.io/blog/2014/07/08/audio-in-ios-2/ 前言 本篇为<iOS音频播放>系列的第二篇. 在实施前一篇中所 ...

  4. WindowFromPoint -- 获得包括指定点的窗体的句柄

     WindowFromPoint 函数功能: 该函数获得包括指定点的窗体的句柄. 函数原型: HWND WindowFromPoint(POINT Point): 參数: Point:指定一个被检 ...

  5. android 获取手机信息工具类

    package com.yqy.yqy_listviewheadview; import android.content.Context; import android.telephony.Telep ...

  6. python爬虫【第1篇】

    一.文件读写 1.打开文件 # 以读文件模式代开new.txt f=open(r"c:\new.txt",“r”) f=open("c:\new.txt",“r ...

  7. POJ 1625 Censored! (AC自己主动机 + 高精度 + DP)

    题目链接:Censored! 解析:AC自己主动机 + 高精度 + 简单DP. 字符有可能会超过128.用map映射一下就可以. 中间的数太大.得上高精度. 用矩阵高速幂会超时,简单的DP就能解决时间 ...

  8. BZOJ1202 [HNOI2005]狡猾的商人 并查集维护前缀和

    1202: [HNOI2005]狡猾的商人 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1935  Solved: 936[Submit][Stat ...

  9. 【bzoj4597】 [Shoi2016]随机序列

    可以发现加减号之间可以互相抵消. 真正加到答案里的只有一些前缀积. 记s[i]为a[1]*a[2]*a[3]...*a[i].那s[i]在答案中出现的次数就是2*3^(n-i-1); 修改一个数只会对 ...

  10. 【poj2774】Long Long Message

    用个分隔符将两个字符串连接起来,再用后缀数组求出height数组的值,找出一个height值最大并且i与i-1的sa值分别在两串字符中就好 #include<algorithm> #inc ...