add<电脑新增>
parent
e00a186f03
commit
760cbdcb10
|
@ -13,7 +13,7 @@ import java.io.IOException;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@WebServlet(urlPatterns = {"/computerList","/deleteComputer"})
|
@WebServlet(urlPatterns = {"/computerList","/deleteComputer","/addComputer"})
|
||||||
public class ComputerServlet extends HttpServlet {
|
public class ComputerServlet extends HttpServlet {
|
||||||
|
|
||||||
IComputerService computerService = new ComputerServiceImpl();
|
IComputerService computerService = new ComputerServiceImpl();
|
||||||
|
@ -32,9 +32,16 @@ public class ComputerServlet extends HttpServlet {
|
||||||
case "/deleteComputer":
|
case "/deleteComputer":
|
||||||
deleteComputer(req, resp);
|
deleteComputer(req, resp);
|
||||||
break;
|
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) {
|
private void deleteComputer(HttpServletRequest req, HttpServletResponse resp) {
|
||||||
String id = req.getParameter("id");
|
String id = req.getParameter("id");
|
||||||
try {
|
try {
|
||||||
|
@ -50,4 +57,17 @@ public class ComputerServlet extends HttpServlet {
|
||||||
req.setAttribute("computers", computers);
|
req.setAttribute("computers", computers);
|
||||||
req.getRequestDispatcher("computerList.jsp").forward(req, resp);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ import java.util.List;
|
||||||
|
|
||||||
public interface IComputerService {
|
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;
|
List<Computer> listComputer() throws SQLException;
|
||||||
|
|
||||||
|
|
|
@ -11,9 +11,10 @@ public class ComputerServiceImpl implements IComputerService {
|
||||||
|
|
||||||
ComputerDao computerDao=new ComputerDao();
|
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
|
@Override
|
||||||
|
|
|
@ -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>
|
|
@ -35,6 +35,6 @@
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
<br/>
|
<br/>
|
||||||
<a href="/computer?action=addForm">Add New Computer</a>
|
<a href="/addComputer">Add New Computer</a>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
Reference in New Issue