实体类

package entity;

public class Product {

    private String id;
private String proName;
private String proType;
private double price;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getProName() {
return proName;
}
public void setProName(String proName) {
this.proName = proName;
}
public String getProType() {
return proType;
}
public void setProType(String proType) {
this.proType = proType;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public Product(String id, String proName, String proType, double price) {
super();
this.id = id;
this.proName = proName;
this.proType = proType;
this.price = price;
}
public Product() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Product [id=" + id + ", price=" + price + ", proName="
+ proName + ", proType=" + proType + "]";
} }

商品列表servlet

package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List; import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import dao.ProductDao;
import entity.Product;
/*
* 查询所有商品的servlet
*/
public class ListServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html; charset=utf-8"); ProductDao dao = new ProductDao();
List<Product> list = dao.findAll(); //2.把商品显示到浏览器
PrintWriter writer = response.getWriter();
String html = ""; html += "<html>";
html += "<head>";
html += "<title>显示商品列表</title>";
html += "</head>";
html += "<body>";
html += "<table border='1' align='center' width='600px'>";
html += "<tr>";
html += "<th>编号</th><th>商品名称</th><th>商品型号</th><th>商品价格</th>";
html += "</tr>";
//遍历商品
if (list != null) {
for(Product p : list) {
html += "<tr>";
html += "<td>"+p.getId()+"</td>" +
"<td><a href='"+request.getContextPath()+"/DetailServlet?id="+p.getId()+"'>"
+p.getProName()+"</a></td><td>"+p.getProType()+"</td><td>"+p.getPrice()+"</td>";
html += "<tr>";
}
}
html += "</table>"; /**
* 显示浏览过的商品
*/
html += "最近浏览过的商品:<br/>";
Cookie[] cookies = request.getCookies();// 获取cookie对象
if (cookies != null) {
for(Cookie cookie : cookies) {
if (cookie.getName().equals("prodHist")) {
String prodHist = cookie.getValue();
String[] ids = prodHist.split(",");
for (String id : ids) {
//查询数据库,查询对应的商品
Product p = dao.findById(id);
//显示到浏览器
html += "" + p.getId() + "&nbsp;" + p.getProName() + "&nbsp;" + p.getPrice() + "<br/>";
}
}
}
}
html += "</body>";
html += "</html>"; writer.write(html);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
} }

商品详情

package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Collection;
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; import dao.ProductDao;
import entity.Product;
/**
* 显示商品详细
* @author Administrator
*
*/
public class DetailServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html; charset=utf-8"); //1.获取编号
String id = request.getParameter("id"); //2.到数据库中查询对应编号的商品
ProductDao dao = new ProductDao();
Product product = dao.findById(id); //3.显示到浏览器
PrintWriter writer = response.getWriter();
String html = ""; html += "<html>";
html += "<head>";
html += "<title>显示商品详细</title>";
html += "</head>";
html += "<body>";
html += "<table border='1' align='center' width='300px'>";
if(product!=null){
html += "<tr><th>编号:</th><td>"+product.getId()+"</td></tr>";
html += "<tr><th>商品名称:</th><td>"+product.getProName()+"</td></tr>";
html += "<tr><th>商品型号:</th><td>"+product.getProType()+"</td></tr>";
html += "<tr><th>商品价格:</th><td>"+product.getPrice()+"</td></tr>";
} html += "</table>";
html += "<center><a href='"+request.getContextPath()+"/ListServlet'>[返回列表]</a></center>";
html += "</body>";
html += "</html>"; writer.write(html); /**
* 创建cookie,并发送
*/
//1.创建cookie
Cookie cookie = new Cookie("prodHist", createValue(request, id));
cookie.setMaxAge(1*60*60);//一个月
//2.发送cookie
response.addCookie(cookie);
} /*
* 生成cookie的值
* 分析:
* 当前cookie值 传入商品id 最终cookie值
* null或没有prodHist 1 1 (算法: 直接返回传入的id )
* 1 2 2,1 (没有重复且小于3个。算法:直接把传入的id放最前面 )
* 2,1 1 1,2(有重复且小于3个。算法:去除重复id,把传入的id放最前面 )
* 3,2,1 2 2,3,1(有重复且3个。算法:去除重复id,把传入的id放最前面)
* 3,2,1 4 4,3,2(没有重复且3个。算法:去最后的id,把传入的id放最前面)
* @return
*/
public String createValue(HttpServletRequest request, String id) {
Cookie[] cookies = request.getCookies();
String prodhist = null; if (cookies != null) {
for(Cookie cookie : cookies) {
if(cookie.getName().equals("prodhist")) {
prodhist = cookie.getValue();
break;
}
}
} // null或没有prodHist
if (cookies == null || prodhist == null) {
// 直接返回传入的id
return id;
} // 3,21 2
//String -> String[] -> Collection :为了方便判断重复id
String[] ids = prodhist.split(",");
Collection<String> colls = Arrays.asList(ids); // LinkedList 方便地操作(增删改元素)集合
// Collection -> LinkedList
LinkedList<String> list = new LinkedList<String>(colls); //不超过3个
if(list.size() < 3) {
if(list.contains(id)) {
//去除重复id,把传入的id放最前面
list.remove(id);
list.addFirst(id);
} else {
//直接把传入的id放最前面
list.addFirst(id);
}
} else {
//等于3个
//id重复
if(list.contains(id)){
//去除重复id,把传入的id放最前面
list.remove(id);
list.addFirst(id);
}else{
//去最后的id,把传入的id放最前面
list.removeLast();
list.addFirst(id);
}
} // LinedList -> String
StringBuffer sb = new StringBuffer();
for (Object obj : list) {
sb.append(obj + ",");
}
//去掉最后的逗号
String result = sb.toString();
result = result.substring(0, result.length()-1); return result;
} }

dao

package dao;

import java.util.ArrayList;
import java.util.List; import entity.Product; public class ProductDao { //模拟"数据库",存放所有商品数据
private static List<Product> data = new ArrayList<Product>(); /**
* 初始化商品数据
*/
static {
//只执行一次
for(int i=1; i<=10; i++) {
data.add(new Product("" + i, "笔记本" + i , "LN00" + i,34.0 + i ));
}
} /**
* 提供查询所有商品的方法
*/
public List<Product> findAll() {
return data;
} /**
* 提供根据编号查询商品的方法
*/
public Product findById(String id) {
for (Product p : data) {
if(p.getId().equals(id)) {
return p;
}
}
return null;
}
}

最新文章

  1. 5.1---二进制数插入(CC150)
  2. Java集合中Map接口的使用方法
  3. Linux 环境变量配置
  4. Roman to Integer -- LeetCode 13
  5. mysql取代rand()的高效率随机读取方法
  6. IMP数据到指定的表空间
  7. 安装rlwrap错误的问题解决方法
  8. 【JavaScript】深入理解JavaScript之强大的原型和原型链
  9. ArcEngine部分工作总结
  10. PHP获得文件创建、修改、访问时间 filectime() filemtime() fileatime()
  11. 来自projecteuler.net网站的练习题2
  12. 【模板】字符串匹配的三种做法(Hash、KMP、STL)
  13. FFmpeg命令行工具学习(二):播放媒体文件的工具ffplay
  14. mssql sqlserver in 关键字在值为null的应用举例
  15. 论文笔记:Visual Question Answering as a Meta Learning Task
  16. adb 安装apk到只当设备
  17. 【CF708E】Student&#39;s Camp 组合数+动态规划
  18. SVN:项目管理工具
  19. kubeadm方式安装kubernetes
  20. 一个对比各种开源库的网站 libhunt

热门文章

  1. Spring Boot中微信全局token的缓存实现
  2. (源代码分析)Android-Universal-Image-Loader (图片异步载入缓存库)的使用配置
  3. opencv基础笔记(1)
  4. Echarts Binning on map 根据真实经纬度渲染数据
  5. Linux进程间通信 共享内存+信号量+简单样例
  6. libXdmcp.so.6
  7. win32gui.EnumWindows my.os.EnumWindows.py
  8. 关于spring配置文件中编辑时没有提示信息的问题
  9. android 反编译 for mac
  10. go语言---defer