1. 创建一个JSP页面buy.jsp,实现一个查询蛋糕信息的按钮和表格。当用户点击查询蛋糕信息按钮时,通过axios向后端发送一个请求,后端从数据库中查询所有蛋糕信息并将结果返回给前端。前端通过axios接收后端传回的数据,并将数据填充到表格中。 2. 在页面下方添加一个购物车区域,包括一个蛋糕列表和一个购买按钮。当用户复选蛋糕后,将其添加到蛋糕列表中。当用户点击购买按钮时,通过axios向后端发送一个请求,提交所购买的蛋糕信息。后端接收请求后,更新数据库中的剩余蛋糕数量,并将购买的蛋糕信息返回给前端。前端通过axios接收后端传回的数据,并将其填充到购物车列表中。 3. 在后端使用连接池管理数据库连接,并使用JDBC API连接数据库。实现查询所有蛋糕信息、更新蛋糕数量等操作。
时间: 2023-06-12 22:04:52 浏览: 347
基于JSP+MYSQL的路况信息查询系统.zip
好的,下面是一个简单的实现思路和代码示例。
1. 在buy.jsp中添加以下HTML代码:
```html
<button onclick="queryCakes()">查询蛋糕信息</button>
<table id="cakeTable">
<tr>
<th>蛋糕名称</th>
<th>蛋糕价格</th>
<th>蛋糕数量</th>
</tr>
</table>
<div>
<h3>购物车</h3>
<table id="cartTable">
<tr>
<th>蛋糕名称</th>
<th>蛋糕价格</th>
<th>蛋糕数量</th>
</tr>
</table>
<button onclick="buyCakes()">购买</button>
</div>
```
2. 在buy.jsp中添加以下JavaScript代码:
```javascript
function queryCakes() {
axios.get('/cakes').then(response => {
const cakes = response.data;
const table = document.getElementById('cakeTable');
cakes.forEach(cake => {
const row = table.insertRow();
row.insertCell().textContent = cake.name;
row.insertCell().textContent = cake.price;
row.insertCell().textContent = cake.quantity;
});
});
}
const cart = [];
function addToCart(name, price, quantity) {
const item = { name, price, quantity };
cart.push(item);
}
function buyCakes() {
axios.post('/buy', cart).then(response => {
const boughtCakes = response.data;
const table = document.getElementById('cartTable');
boughtCakes.forEach(cake => {
const row = table.insertRow();
row.insertCell().textContent = cake.name;
row.insertCell().textContent = cake.price;
row.insertCell().textContent = cake.quantity;
});
});
}
```
3. 在后端使用连接池管理数据库连接,并使用JDBC API连接数据库。在Servlet中添加以下代码:
```java
import java.sql.Connection;
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.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import com.fasterxml.jackson.databind.ObjectMapper;
public class CakeServlet extends HttpServlet {
private DataSource dataSource;
@Override
public void init() throws ServletException {
try {
Context context = new InitialContext();
dataSource = (DataSource) context.lookup("java:comp/env/jdbc/cake");
} catch (NamingException e) {
throw new ServletException("Unable to initialize DataSource", e);
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try (Connection connection = dataSource.getConnection()) {
PreparedStatement statement = connection.prepareStatement("SELECT * FROM cakes");
ResultSet resultSet = statement.executeQuery();
List<Cake> cakes = new ArrayList<>();
while (resultSet.next()) {
Cake cake = new Cake();
cake.setId(resultSet.getInt("id"));
cake.setName(resultSet.getString("name"));
cake.setPrice(resultSet.getDouble("price"));
cake.setQuantity(resultSet.getInt("quantity"));
cakes.add(cake);
}
ObjectMapper mapper = new ObjectMapper();
response.setContentType("application/json");
mapper.writeValue(response.getOutputStream(), cakes);
} catch (SQLException e) {
throw new ServletException("Unable to query cakes", e);
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try (Connection connection = dataSource.getConnection()) {
List<Cake> cart = new ObjectMapper().readValue(request.getInputStream(),
new TypeReference<List<Cake>>() {});
for (Cake cake : cart) {
PreparedStatement statement = connection.prepareStatement(
"UPDATE cakes SET quantity = quantity - ? WHERE id = ?");
statement.setInt(1, cake.getQuantity());
statement.setInt(2, cake.getId());
statement.executeUpdate();
}
ObjectMapper mapper = new ObjectMapper();
response.setContentType("application/json");
mapper.writeValue(response.getOutputStream(), cart);
} catch (SQLException e) {
throw new ServletException("Unable to buy cakes", e);
}
}
}
```
这里假设数据库中有一个名为cakes的表,包含id、name、price和quantity四个字段。在web.xml中配置数据源:
```xml
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/cake</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<resource-env-ref>
<description>DB Connection Pool</description>
<resource-env-ref-name>jdbc/cake</resource-env-ref-name>
<resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type>
</resource-env-ref>
<resource-env-ref>
<description>DB Connection Properties</description>
<resource-env-ref-name>jdbc/cake/properties</resource-env-ref-name>
<resource-env-ref-type>java.util.Properties</resource-env-ref-type>
</resource-env-ref>
<resource-ref>
<description>DB Connection Pool</description>
<res-ref-name>jdbc/cake</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<mapped-name>java:comp/env/jdbc/cake</mapped-name>
<lookup-name>jdbc/cake</lookup-name>
</resource-ref>
```
以上代码示例仅为参考,实际实现中需要根据具体需求进行修改。
阅读全文