add<电脑新增>

master
liyansheng 2024-12-21 01:42:43 +08:00
parent e00a186f03
commit 760cbdcb10
5 changed files with 49 additions and 5 deletions

View File

@ -13,7 +13,7 @@ import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
@WebServlet(urlPatterns = {"/computerList","/deleteComputer"})
@WebServlet(urlPatterns = {"/computerList","/deleteComputer","/addComputer"})
public class ComputerServlet extends HttpServlet {
IComputerService computerService = new ComputerServiceImpl();
@ -32,9 +32,16 @@ public class ComputerServlet extends HttpServlet {
case "/deleteComputer":
deleteComputer(req, resp);
break;
case "/addComputer":
addComputer(req, resp);
break;
}
}
private void addComputer(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getRequestDispatcher("addComputer.jsp").forward(req, resp);
}
private void deleteComputer(HttpServletRequest req, HttpServletResponse resp) {
String id = req.getParameter("id");
try {
@ -50,4 +57,17 @@ public class ComputerServlet extends HttpServlet {
req.setAttribute("computers", computers);
req.getRequestDispatcher("computerList.jsp").forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String name = req.getParameter("name");
String price = req.getParameter("price");
String stock = req.getParameter("stock");
computerService.addComputer(name,Double.parseDouble(price),Integer.parseInt(stock));
try {
resp.sendRedirect("/computerList");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -7,7 +7,7 @@ import java.util.List;
public interface IComputerService {
public void addComputer(String name, String brand, String type, String price);
public void addComputer(String name, Double price, int stock);
List<Computer> listComputer() throws SQLException;

View File

@ -11,9 +11,10 @@ public class ComputerServiceImpl implements IComputerService {
ComputerDao computerDao=new ComputerDao();
@Override
public void addComputer(String name, String brand, String type, String price) {
@Override
public void addComputer(String name, Double price, int stock) {
computerDao.addComputer(name, price, stock);
}
@Override

View File

@ -0,0 +1,23 @@
<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add Computer</title>
</head>
<body>
<h1>Add New Computer</h1>
<form action="/addComputer" method="post">
<label for="name">Name: </label><br>
<input type="text" id="name" name="name"><br><br>
<label for="price">Price: </label><br>
<input type="text" id="price" name="price"><br><br>
<label for="stock">Stock: </label><br>
<input type="text" id="stock" name="stock"><br><br>
<input type="submit" value="Add Computer">
</form>
<br>
<a href="/computerList">Back to List</a>
</body>
</html>

View File

@ -35,6 +35,6 @@
</tbody>
</table>
<br/>
<a href="/computer?action=addForm">Add New Computer</a>
<a href="/addComputer">Add New Computer</a>
</body>
</html>