1.创建商品类,包括字段:id,name,type,brand,完善构造函数及get和set方法
2.创建一个servlet,在其中构造一个商品的ArrayList,并创建至少4个商品。将商品列表放入request中,将商品个数放到cookie中,并转发到list.jsp
3.在list.jsp中用el表达式和jstl商品的所有信息显示在表格中,将cookie中数据取出并显示
部分基于chartgpt编写
public class Product {
private int id;
private String name;
private String type;
private String brand;
public Product(int id, String name, String type, String brand) {
this.id = id;
this.name = name;
this.type = type;
this.brand = brand;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
}
import java.util.ArrayList;
import javax.servlet.http.*;
public class ProductServlet extends HttpServlet {
ArrayList<Product> productList = new ArrayList<>();
public void init() {
Product p1 = new Product(1, "iPhone X", "Mobile Phone", "Apple");
Product p2 = new Product(2, "iPad Pro", "Tablet", "Apple");
Product p3 = new Product(3, "Surface Pro", "Tablet", "Microsoft");
Product p4 = new Product(4, "ThinkPad X1 Carbon", "Laptop", "Lenovo");
productList.add(p1);
productList.add(p2);
productList.add(p3);
productList.add(p4);
}
public void doGet(HttpServletRequest request,
HttpServletResponse response) {
Cookie cookie = new Cookie("productCount", Integer.toString(productList.size()));
response.addCookie(cookie);
request.setAttribute("productList", productList);
request.getRequestDispatcher("list.jsp").forward(request, response);
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Product List</title>
</head>
<body>
<table border="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Type</th>
<th>Brand</th>
</tr>
<c:forEach var="product" items="${productList}">
<tr>
<td>${product.id}</td>
<td>${product.name}</td>
<td>${product.type}</td>
<td>${product.brand}</td>
</tr>
</c:forEach>
</table>
<p>Product Count: ${cookie.productCount.value}</p>
</body>
</html>
如果以上回答对您有所帮助,点击一下采纳该答案~谢谢
public class Product {
private int id;
private String name;
private String type;
private String brand;
public Product(int id, String name, String type, String brand) {
this.id = id;
this.name = name;
this.type = type;
this.brand = brand;
}
// Getters and setters
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
}
```java
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ProductServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 创建商品列表
ArrayList<Product> productList = new ArrayList<Product>();
productList.add(new Product(1, "iPhone 12", "智能手机", "苹果"));
productList.add(new Product(2, "Galaxy S21", "智能手机", "三星"));
productList.add(new Product(3, "iPad Pro", "平板电脑", "苹果"));
productList.add(new Product(4, "Galaxy Tab S7", "平板电脑", "三星"));
// 将商品列表添加到request中
request.setAttribute("productList", productList);
// 将商品个数放到cookie中
Cookie productCountCookie = new Cookie("productCount", String.valueOf(productList.size()));
response.addCookie(productCountCookie);
// 转发到list.jsp
request.getRequestDispatcher("list.jsp").forward(request, response);
}
}
3. 在list.jsp中用el表达式和jstl商品的所有信息显示在表格中,将cookie中数据取出并显示。
```java
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>商品列表</title>
</head>
<body>
<h1>商品列表</h1>
<p>共有<c:out value="${cookie.productCount.value}" />件商品</p>
<table border="1">
<tr>
<th>ID</th>
<th>名称</th>
<th>类型</th>
<th>品牌</th>
</tr>
<c:forEach items="${requestScope.productList}" var="product">
<tr>
<td><c:out value