使用javaweb中session实现购物车代码
时间: 2024-06-11 17:09:09 浏览: 128
以下是使用JavaWeb中session实现购物车的代码示例:
1. 创建商品类
```java
public class Product {
private int id; // 商品ID
private String name; // 商品名称
private double price; // 商品价格
private int quantity; // 商品数量
public Product(int id, String name, double price, int quantity) {
this.id = id;
this.name = name;
this.price = price;
this.quantity = quantity;
}
// setter和getter方法省略
}
```
2. 创建购物车类
```java
import java.util.ArrayList;
import java.util.List;
public class ShoppingCart {
private List<Product> productList; // 商品列表
public ShoppingCart() {
productList = new ArrayList<>();
}
// 添加商品到购物车
public void addProduct(Product product) {
productList.add(product);
}
// 从购物车中删除商品
public void removeProduct(Product product) {
productList.remove(product);
}
// 获取购物车中的所有商品
public List<Product> getProductList() {
return productList;
}
// 获取购物车中商品的总价
public double getTotalPrice() {
double totalPrice = 0;
for (Product product : productList) {
totalPrice += product.getPrice() * product.getQuantity();
}
return totalPrice;
}
}
```
3. 在Servlet中使用session实现购物车功能
```java
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 javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet(name = "ShoppingCartServlet", urlPatterns = "/shoppingCart")
public class ShoppingCartServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
// 获取购物车对象
ShoppingCart shoppingCart = (ShoppingCart) session.getAttribute("shoppingCart");
if (shoppingCart == null) { // 如果session中不存在购物车对象,则创建一个
shoppingCart = new ShoppingCart();
session.setAttribute("shoppingCart", shoppingCart);
}
// 获取操作类型
String action = request.getParameter("action");
// 添加商品到购物车
if ("add".equals(action)) {
int productId = Integer.parseInt(request.getParameter("productId"));
String productName = request.getParameter("productName");
double productPrice = Double.parseDouble(request.getParameter("productPrice"));
int quantity = Integer.parseInt(request.getParameter("quantity"));
Product product = new Product(productId, productName, productPrice, quantity);
shoppingCart.addProduct(product);
response.sendRedirect("productList.jsp");
}
// 从购物车中删除商品
if ("remove".equals(action)) {
int productId = Integer.parseInt(request.getParameter("productId"));
for (Product product : shoppingCart.getProductList()) {
if (product.getId() == productId) {
shoppingCart.removeProduct(product);
break;
}
}
response.sendRedirect("shoppingCart.jsp");
}
// 清空购物车
if ("clear".equals(action)) {
shoppingCart = new ShoppingCart();
session.setAttribute("shoppingCart", shoppingCart);
response.sendRedirect("shoppingCart.jsp");
}
}
}
```
4. 在JSP页面中显示购物车信息
```jsp
<%@page contentType="text/html" 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>
<table border="1">
<thead>
<tr>
<th>商品ID</th>
<th>商品名称</th>
<th>商品价格</th>
<th>商品数量</th>
<th>总价</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach var="product" items="${sessionScope.shoppingCart.productList}">
<tr>
<td>${product.id}</td>
<td>${product.name}</td>
<td>${product.price}</td>
<td>${product.quantity}</td>
<td>${product.price * product.quantity}</td>
<td>
<form action="shoppingCart" method="post">
<input type="hidden" name="action" value="remove" />
<input type="hidden" name="productId" value="${product.id}" />
<input type="submit" value="删除" />
</form>
</td>
</tr>
</c:forEach>
<tr>
<td colspan="4">总价:</td>
<td>${sessionScope.shoppingCart.totalPrice}</td>
<td>
<form action="shoppingCart" method="post">
<input type="hidden" name="action" value="clear" />
<input type="submit" value="清空购物车" />
</form>
</td>
</tr>
</tbody>
</table>
<a href="productList.jsp">继续购物</a>
</body>
</html>
```
以上就是使用JavaWeb中session实现购物车的代码示例。
阅读全文