id是指浏览过的商品id
public class Product {
private String id;
private String name;
private Double price;
private String description;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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;
}
@Override
public String toString() {
return "Product{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
", description='" + description + '\'' +
'}';
}
public Product(String id, String name, Double price, String description) {
this.id = id;
this.name = name;
this.price = price;
this.description = description;
}
}
商品信息类
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@WebServlet(value = "/index",loadOnStartup = 1)//让init()在应用启动时被调用
public class IndexServlet extends HttpServlet {
@Override
public void init() throws ServletException {
//准备一些商品数据 用这种方式来模拟从数据里面获取数据
// alt + 鼠标左键 纵向选择
Product product1 = new Product("1", "iphone12", 5600.0, "new version of iphone");
Product product2 = new Product("2", "Mi11", 5200.0, "Mi flagship version");
Product product3 = new Product("3", "Huawei Mate40", 4500.0, "Huawei");
Product product4 = new Product("4", "One Plus 9", 6000.0, "One Plus");
List<Product> products = new ArrayList<>();
products.add(product1);
products.add(product2);
products.add(product3);
products.add(product4);
//如何将products和其他servlet进行共享 context域
getServletContext().setAttribute("products", products);
}
// 在首页显示商品名字
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
List<Product> products = (List<Product>) getServletContext().getAttribute("products");
for (Product product : products) {
//在商品名字上加上超链接,可以点进去看商品详情
response.getWriter().println("<div><a href='"+ request.getContextPath() + "/detail?id=" + product.getId() + "'>" + product.getName() + "</a></div>");
}
response.getWriter().println("您的历史访问足迹为:");
History.showView(request, response);
}
}
首页显示
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@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 {
//首先需要拿到id的值,商品的其他信息拿到(和context域里面的数据进行比对)
// requestURI - contextPath = servletPath /detail
response.setContentType("text/html;charset=utf-8");
String id = request.getParameter("id");
//把请求给历史记录,更新历史记录
History.updateView(request);
List<Product> products = (List<Product>) getServletContext().getAttribute("products");
for (Product product : products) {
if(id.equals(product.getId())){
response.getWriter().println(product);
}
}
//实现3个超链接
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>");
}
}
商品详情
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
public class History {
private static final int CAPACITY = 3;//历史记录最多显示3条
//更新历史记录
public static void updateView(HttpServletRequest request) {
String id = request.getParameter("id");//获得当前访问的商品id
HttpSession session = request.getSession();//每个用户有一个历史记录集合
//历史足迹和用户是相关的,所以应该使用session
//request:非常小; 三个以上页面使用不可能使用转发
//context:只要涉及到和用户相关的,那么肯定不能用
LinkedList<String> history = (LinkedList<String>) session.getAttribute("history");
if(history == null){
//因为历史记录要经常做增删操作,用链表更合适
history = new LinkedList<>();
session.setAttribute("history", history);
}
//把id存入到list中 1.当前id已经在链表内 2.当前id不在链表内 哪个情况需要考虑会不会超出history的容量
if(history.contains(id)){
history.remove(id);
}else {
if(history.size() == CAPACITY){
history.removeLast();
}
}
history.addFirst(id);
}
//显示历史记录
//就是从history出取出id,然后通过id拿到id对应的其他信息
public static void showView(HttpServletRequest request, HttpServletResponse response) throws IOException {
LinkedList<String> history = (LinkedList) request.getSession().getAttribute("history");
if(history != null){
List<Product> products = (List<Product>) request.getServletContext().getAttribute("products");
for (String id : history) {
for (Product product : products) {
if(id.equals(product.getId())){
String detail = response.encodeURL(request.getContextPath() + "/detail?id=" + product.getId());
response.getWriter().println("<div><a href='" + detail + "'>" + product.getName() + "</a></div>");
}
}
}
}
}
}
历史记录处理逻辑