这里写目录标题
实现一个简易版的历史足迹(比如再首页回显示出最近浏览的商品信息,可以不按照时间降序来排列,仅显示出来,可以不设置足迹的大小限制,比如最近访问的数据都会显示出来,没有最近多少条的限制)
结构:
IndexServlet.java
@WebServlet(value = "/index9",loadOnStartup = 1)
public class IndexServlet extends HttpServlet {
@Override
public void init() throws ServletException {
//创建一个数组products用来存放汽车
List<Product> products = new ArrayList<>();
//创建model3、nio、xpeng、li等汽车对象
Product model3 = new Product("1", "Tesla Model 3", 240000.0, "国产Tesla model3");
Product nio = new Product("2", "Nio ES6", 450000.0, "国产电动车");
Product xpeng = new Product("3", "Xpeng P7", 230000.0, "国产电动");
Product li = new Product("4", "Li one", 250000.0, "国产电动三");
//将创建出的model3、nio、xpeng、li等汽车对象加入products数组存放
products.add(model3);
products.add(nio);
products.add(xpeng);
products.add(li);
//为ServletContext的属性products设置值
getServletContext().setAttribute("products", products);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//显示出来
response.setContentType("text/html;charset=utf-8");
//将ServletContext的属性products取出,并创建数组对象products
List<Product> products = (List<Product>) getServletContext().getAttribute("products");
//面向浏览器显示
response.getWriter().println("<!DOCTYPE html>\n" +
"<html lang=\"en\">\n" +
"<head>\n" +
" <meta charset=\"UTF-8\">\n" +
" <title>Title</title>\n" +
"</head>\n" +
"<body>");
//遍历输出数组中的汽车数据
for (Product product : products) {
//response.encodeURL把字符串作为 URI 进行编码
// (访问路径+ "/目标目录?id=" + 产品id());
String detail = response.encodeURL(request.getContextPath() + "/detail?id=" + product.getId());
// ("<div><a href='" +访问路径对象 + "'>" + 产品名称 + "</a></div>");
response.getWriter().println("<div><a href='" + detail + "'>" + product.getName() + "</a></div>");
}
response.getWriter().println("浏览足迹为:");
//显示浏览记录
History.showRecentViews(request, response);
response.getWriter().println("\n" +
"</body>\n" +
"</html>");
}
}
Product.java
public class Product {
private String id;
private String name;
private Double price;
private String description;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
public Product(String id, String name, Double price, String description) {
this.id = id;
this.name = name;
this.price = price;
this.description = description;
}
@Override
public String toString() {
return "Product{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
", description='" + description + '\'' +
'}';
}
}
AddCartServlet.java
@WebServlet("/addCart")
public class AddCartServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
String id = request.getParameter("id");
HttpSession session = request.getSession();
//判断是否为空 还可以考虑加上数量
// list
//要先取出来,再塞进去
List<String> cart = (List<String>) session.getAttribute("cart");
if(cart == null){
cart = new ArrayList<>();
session.setAttribute("cart", cart);
}
cart.add(id);
response.getWriter().println("加入购物车成功,即将跳转回首页");
String index = response.encodeURL(request.getContextPath() + "/index9");
response.setHeader("refresh", "2;url=" + index );
}
}
DetailServlet.java
@WebServlet("/detail")
public class DetailServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
//获取id属性参数
String id = request.getParameter("id");
//判断是否为空
History.updateRecentViews(request);
List<Product> products = (List<Product>) getServletContext().getAttribute("products");
for (Product product : products) {
if(product.getId().equals(id)){
response.getWriter().println(product);
}
}
String index = response.encodeURL(request.getContextPath() + "/index");
String addCart = response.encodeURL(request.getContextPath() + "/addCart?id=" + id);
String viewCart = response.encodeURL(request.getContextPath() + "/viewCart");
response.getWriter().println("<a href='" + index + "'>返回首页</a>");
response.getWriter().println("<a href='" + addCart + "'>加入购物车</a>");
response.getWriter().println("<a href='" + viewCart + "'>查看购物车</a>");
}
}
History.java
// LRU:latest recent usage 内存里面
public class History {
private static final int CAPACITY = 3;
//展示最近记录
public static void showRecentViews(HttpServletRequest request, HttpServletResponse response) throws IOException {
LinkedList<String> history = (LinkedList) request.getSession().getAttribute("history");
// ids
List<Product> products = (List<Product>) request.getServletContext().getAttribute("products");
for (String id : history) {
for (Product product : products) {
if(id.equals(product.getId())){
response.getWriter().println("<span><a href='" + request.getContextPath() + "/detail?id=" + product.getId() + "'>" + product.getName() + "</a>");
}
}
}
}
public static void updateRecentViews(HttpServletRequest request) {
String id = request.getParameter("id");
//顺序的Linkedlist
HttpSession session = request.getSession();
LinkedList history = (LinkedList) session.getAttribute("history");
if(history == null){
history = new LinkedList();
session.setAttribute("history", history);
}
// 1当前history size是否已经满了 2当前的id是否已经在里面了 切入点
if(history.contains(id)){
history.remove(id);
}else {
if(history.size() == CAPACITY){
history.removeLast();
}
}
history.addFirst(id);
}
}
ViewCartServlet.java
@WebServlet("/viewCart")
public class ViewCartServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
HttpSession session = request.getSession();
List<String> cart = (List<String>) session.getAttribute("cart");
if(cart == null){
response.getWriter().println("购物车为空");
return;
}
List<Product> products = (List<Product>) getServletContext().getAttribute("products");
for (Product product : products) {
for (String id : cart) {
if(id.equals(product.getId())){
response.getWriter().println(product);
}
}
}
String index = response.encodeURL(request.getContextPath() + "/index9");
response.getWriter().println("<a href='" + index + "'>返回首页</a>");
}
}
结果: