用JavaWeb写个购物车代码
时间: 2023-05-13 11:06:51 浏览: 122
好的,以下是一个简单的JavaWeb购物车代码:
1. 创建一个名为 "ShoppingCart" 的Java类,用于存储购物车中的商品信息。
```java
public class ShoppingCart {
private Map<String, Integer> items = new HashMap<>();
public void addItem(String itemId, int quantity) {
if (items.containsKey(itemId)) {
int currentQuantity = items.get(itemId);
items.put(itemId, currentQuantity + quantity);
} else {
items.put(itemId, quantity);
}
}
public void removeItem(String itemId) {
items.remove(itemId);
}
public Map<String, Integer> getItems() {
return items;
}
public int getTotalQuantity() {
int totalQuantity = 0;
for (int quantity : items.values()) {
totalQuantity += quantity;
}
return totalQuantity;
}
public double getTotalPrice() {
double totalPrice = 0.0;
for (Map.Entry<String, Integer> entry : items.entrySet()) {
String itemId = entry.getKey();
int quantity = entry.getValue();
double itemPrice = getItemPrice(itemId);
totalPrice += itemPrice * quantity;
}
return totalPrice;
}
private double getItemPrice(String itemId) {
// TODO: 根据商品ID获取商品价格
return 0.0;
}
}
```
2. 创建一个名为 "ShoppingCartServlet" 的Java类,用于处理购物车相关的HTTP请求。
```java
@WebServlet("/cart")
public class ShoppingCartServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
ShoppingCart cart = (ShoppingCart) session.getAttribute("cart");
if (cart == null) {
cart = new ShoppingCart();
session.setAttribute("cart", cart);
}
request.getRequestDispatcher("/WEB-INF/views/cart.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
ShoppingCart cart = (ShoppingCart) session.getAttribute("cart");
if (cart == null) {
cart = new ShoppingCart();
session.setAttribute("cart", cart);
}
String itemId = request.getParameter("itemId");
int quantity = Integer.parseInt(request.getParameter("quantity"));
cart.addItem(itemId, quantity);
response.sendRedirect(request.getContextPath() + "/cart");
}
}
```
3. 创建一个名为 "cart.jsp" 的JSP页面,用于显示购物车中的商品信息和总价。
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>购物车</title>
</head>
<body>
<h1>购物车</h1>
<table>
<thead>
<tr>
<th>商品ID</th>
<th>商品名称</th>
<th>数量</th>
<th>单价</th>
<th>小计</th>
</tr>
</thead>
<tbody>
<% ShoppingCart cart = (ShoppingCart) session.getAttribute("cart"); %>
<% for (Map.Entry<String, Integer> entry : cart.getItems().entrySet()) { %>
<tr>
<td><%= entry.getKey() %></td>
<td><%= getItemName(entry.getKey()) %></td>
<td><%= entry.getValue() %></td>
<td><%= getItemPrice(entry.getKey()) %></td>
<td><%= getItemPrice(entry.getKey()) * entry.getValue() %></td>
</tr>
<% } %>
</tbody>
<tfoot>
<tr>
<td colspan="4">总计</td>
<td><%= cart.getTotalPrice() %></td>
</tr>
</tfoot>
</table>
<form method="post" action="<%= request.getContextPath() %>/cart">
<label for="itemId">商品ID:</label>
<input type="text" name="itemId" id="itemId">
<label for="quantity">数量:</label>
<input type="number" name="quantity" id="quantity" min="1" value="1">
<input type="submit" value="添加到购物车">
</form>
</body>
</html>
<%-- 获取商品名称 --%>
<%
private String getItemName(String itemId) {
// TODO: 根据商品ID获取商品名称
return "";
}
%>
<%-- 获取商品价格 --%>
<%
private double getItemPrice(String itemId) {
// TODO: 根据商品ID获取商品价格
return 0.0;
}
%>
```
以上是一个简单的JavaWeb购物车代码,仅供参考。
阅读全文