设计一个商品信息HTML页面,包含商品名称、单价、数量三个参数,直接提交到服务器端JSP页面,要求在该页面显示当前商品的三个参数信息并保存到数据库中。
时间: 2024-02-24 17:53:49 浏览: 26
以下是一个简单的实现:
1. 商品信息HTML页面(product.html):
```
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>商品信息</title>
</head>
<body>
<form action="product.jsp" method="post">
<label for="name">商品名称:</label>
<input type="text" id="name" name="name"><br>
<label for="price">单价:</label>
<input type="text" id="price" name="price"><br>
<label for="quantity">数量:</label>
<input type="text" id="quantity" name="quantity"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
```
2. 服务器端JSP页面(product.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>
<c:set var="name" value="${param.name}" />
<c:set var="price" value="${param.price}" />
<c:set var="quantity" value="${param.quantity}" />
<c:if test="${not empty name and not empty price and not empty quantity}">
<p>商品名称:${name}</p>
<p>单价:${price}</p>
<p>数量:${quantity}</p>
<%
String url = "jdbc:mysql://localhost:3306/test";
String username = "root";
String password = "123456";
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, username, password);
String sql = "INSERT INTO product (name, price, quantity) VALUES (?, ?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setDouble(2, Double.parseDouble(price));
pstmt.setInt(3, Integer.parseInt(quantity));
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
%>
<p>商品信息已保存</p>
</c:if>
</body>
</html>
```
在该JSP页面中,首先使用<c:set>标签将请求参数中的商品名称、单价、数量保存到变量中。然后使用<c:if>标签判断这三个变量是否为空,如果不为空则显示商品信息,并将该信息保存到数据库中。需要注意的是,为了防止SQL注入攻击,这里使用了PreparedStatement对象来执行SQL语句,并使用try-catch-finally语句块确保数据库连接和资源的正确关闭。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)