showALLProduct.jsp里面进去展示所有商品信息,表格有四列,最前面为cherkbox复选框,product数据库表有pid,pname,categpry表有cid和cname,第二列为商品id,即pid,第三列为商品名称,即pname,第四列为类别名称,即cname,最后一列为操作,即删除按钮,有productDao类,ProductService类,BaseServlet类代码怎么写
时间: 2024-03-13 14:48:10 浏览: 115
好的,以下是示例代码,其中包括ProductDao类、ProductService类、BaseServlet类和showALLProduct.jsp页面的部分代码。这些代码可以用来展示所有商品信息,并实现商品的删除操作。
ProductDao类(用来与数据库交互,执行CRUD操作):
```java
package com.example.dao;
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 com.example.model.Product;
import com.example.utils.DBUtils;
public class ProductDao {
// 获取所有商品信息
public List<Product> getAllProducts() {
List<Product> productList = new ArrayList<>();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = DBUtils.getConnection();
String sql = "SELECT p.pid, p.pname, c.cname FROM product p JOIN category c ON p.cid=c.cid";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
int pid = rs.getInt("pid");
String pname = rs.getString("pname");
String cname = rs.getString("cname");
Product product = new Product(pid, pname, cname);
productList.add(product);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtils.close(rs, ps, conn);
}
return productList;
}
// 删除商品
public boolean deleteProduct(int pid) {
boolean result = false;
Connection conn = null;
PreparedStatement ps = null;
try {
conn = DBUtils.getConnection();
String sql = "DELETE FROM product WHERE pid=?";
ps = conn.prepareStatement(sql);
ps.setInt(1, pid);
int rows = ps.executeUpdate();
if (rows > 0) {
result = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtils.close(null, ps, conn);
}
return result;
}
}
```
ProductService类(用来处理业务逻辑,调用ProductDao类的方法):
```java
package com.example.service;
import java.util.List;
import com.example.dao.ProductDao;
import com.example.model.Product;
public class ProductService {
private ProductDao productDao = new ProductDao();
// 获取所有商品信息
public List<Product> getAllProducts() {
return productDao.getAllProducts();
}
// 删除商品
public boolean deleteProduct(int pid) {
return productDao.deleteProduct(pid);
}
}
```
BaseServlet类(用来处理 HTTP 请求,根据请求的类型调用 ProductService 类的相应方法):
```java
package com.example.servlet;
import java.io.IOException;
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 com.example.service.ProductService;
@WebServlet("/products/*")
public class BaseServlet extends HttpServlet {
private ProductService productService = new ProductService();
// 处理 GET 请求
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String path = req.getPathInfo();
if (path == null || path.equals("/")) {
// 如果请求的是 /products/,则展示所有商品信息
req.setAttribute("productList", productService.getAllProducts());
req.getRequestDispatcher("/showALLProduct.jsp").forward(req, resp);
}
}
// 处理 POST 请求
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String path = req.getPathInfo();
if (path != null && path.equals("/delete")) {
// 如果请求的是 /products/delete,说明用户要删除某个商品
int pid = Integer.parseInt(req.getParameter("pid"));
productService.deleteProduct(pid);
resp.sendRedirect(req.getContextPath() + "/products/");
}
}
}
```
showALLProduct.jsp页面(展示所有商品信息的表格,包括复选框列、商品ID列、商品名称列、类别名称列和操作列):
```html
<%@ page import="com.example.model.Product" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<title>所有商品信息</title>
<meta charset="UTF-8">
<script>
// 删除商品
function deleteProduct(pid) {
if (confirm("确定要删除该商品吗?")) {
var form = document.createElement("form");
form.method = "POST";
form.action = "<%=request.getContextPath()%>/products/delete";
var input = document.createElement("input");
input.type = "hidden";
input.name = "pid";
input.value = pid;
form.appendChild(input);
document.body.appendChild(form);
form.submit();
}
}
</script>
</head>
<body>
<h1>所有商品信息</h1>
<table>
<tr>
<th>复选框</th>
<th>商品ID</th>
<th>商品名称</th>
<th>类别名称</th>
<th>操作</th>
</tr>
<% List<Product> productList = (List<Product>) request.getAttribute("productList"); %>
<% for (Product product : productList) { %>
<tr>
<td><input type="checkbox" name="pid" value="<%=product.getPid()%>"></td>
<td><%=product.getPid()%></td>
<td><%=product.getPname()%></td>
<td><%=product.getCname()%></td>
<td><button onclick="deleteProduct(<%=product.getPid()%>)">删除</button></td>
</tr>
<% } %>
</table>
</body>
</html>
```
其中,我们在页面中使用了JSP的标签库,以便更方便地访问JavaBean的属性。同时,我们也添加了JavaScript代码,用来实现商品的删除操作。当用户点击某个商品的删除按钮时,会弹出一个确认框,询问用户是否确定要删除该商品。如果用户点击了“确定”按钮,则会提交一个POST请求到 /products/delete,携带商品的ID信息。BaseServlet类会接收到这个请求,并调用ProductService类的deleteProduct()方法来删除商品。最后,页面会重定向到 /products/,重新展示所有商品信息的表格。
阅读全文