用jsp商品订单界面的实现
时间: 2023-10-03 15:03:55 浏览: 93
jsp实现的购物系统
5星 · 资源好评率100%
首先,你需要一个数据库来存储商品信息和订单信息。然后,你可以使用JSP和Servlet来实现商品订单界面。
以下是一个简单的示例:
1. 创建一个数据库表来存储商品信息和订单信息。
```sql
CREATE TABLE products (
id INT PRIMARY KEY,
name VARCHAR(50),
price DECIMAL(10, 2),
description VARCHAR(255)
);
CREATE TABLE orders (
id INT PRIMARY KEY,
product_id INT,
quantity INT,
total_price DECIMAL(10, 2),
customer_name VARCHAR(50),
customer_email VARCHAR(50),
status VARCHAR(20)
);
```
2. 创建一个JSP页面来显示商品列表和订单表单。
```jsp
<%@ 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>
<table>
<tr>
<th>ID</th>
<th>名称</th>
<th>价格</th>
<th>描述</th>
<th>操作</th>
</tr>
<c:forEach items="${products}" var="product">
<tr>
<td>${product.id}</td>
<td>${product.name}</td>
<td>${product.price}</td>
<td>${product.description}</td>
<td>
<form action="order.jsp" method="post">
<input type="hidden" name="product_id" value="${product.id}">
<input type="submit" value="购买">
</form>
</td>
</tr>
</c:forEach>
</table>
<h1>订单表单</h1>
<form action="place_order.jsp" method="post">
<label>商品名称:</label>
<input type="text" name="product_name"><br>
<label>数量:</label>
<input type="text" name="quantity"><br>
<label>客户姓名:</label>
<input type="text" name="customer_name"><br>
<label>客户邮箱:</label>
<input type="text" name="customer_email"><br>
<input type="submit" value="提交订单">
</form>
</body>
</html>
```
3. 创建一个Servlet来获取商品列表并将其传递给JSP页面。
```java
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
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.sql.DataSource;
@WebServlet("/product_list")
public class ProductListServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Product> products = new ArrayList<>();
try {
// 使用JNDI获取数据源
Context context = new InitialContext();
DataSource dataSource = (DataSource) context.lookup("java:comp/env/jdbc/mydb");
// 获取数据库连接
try (Connection conn = dataSource.getConnection()) {
String sql = "SELECT * FROM products";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
Product product = new Product();
product.setId(rs.getInt("id"));
product.setName(rs.getString("name"));
product.setPrice(rs.getBigDecimal("price"));
product.setDescription(rs.getString("description"));
products.add(product);
}
}
}
}
} catch (NamingException | SQLException e) {
throw new ServletException(e);
}
request.setAttribute("products", products);
request.getRequestDispatcher("/product_list.jsp").forward(request, response);
}
}
```
4. 创建一个Servlet来处理用户提交的订单并将其保存到数据库中。
```java
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
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.sql.DataSource;
@WebServlet("/place_order")
public class PlaceOrderServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int productId = Integer.parseInt(request.getParameter("product_id"));
String productName = request.getParameter("product_name");
int quantity = Integer.parseInt(request.getParameter("quantity"));
String customerName = request.getParameter("customer_name");
String customerEmail = request.getParameter("customer_email");
BigDecimal totalPrice = null;
try {
// 使用JNDI获取数据源
Context context = new InitialContext();
DataSource dataSource = (DataSource) context.lookup("java:comp/env/jdbc/mydb");
// 获取数据库连接
try (Connection conn = dataSource.getConnection()) {
String sql = "SELECT price FROM products WHERE id = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, productId);
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
BigDecimal price = rs.getBigDecimal("price");
totalPrice = price.multiply(new BigDecimal(quantity));
}
}
}
sql = "INSERT INTO orders (product_id, quantity, total_price, customer_name, customer_email, status) VALUES (?, ?, ?, ?, ?, ?)";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, productId);
stmt.setInt(2, quantity);
stmt.setBigDecimal(3, totalPrice);
stmt.setString(4, customerName);
stmt.setString(5, customerEmail);
stmt.setString(6, "未处理");
stmt.executeUpdate();
}
}
} catch (NamingException | SQLException e) {
throw new ServletException(e);
}
request.setAttribute("total_price", totalPrice);
request.getRequestDispatcher("/order_result.jsp").forward(request, response);
}
}
```
5. 创建一个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>
<p>订单已提交,总价为 ${total_price}。</p>
</body>
</html>
```
以上是一个简单的JSP商品订单界面的实现。你可以根据实际需求进行修改和扩展。
阅读全文