2024-12-21 01:22:44 +08:00
|
|
|
package example.controller;
|
|
|
|
|
|
|
|
import example.model.Computer;
|
|
|
|
import example.service.IComputerService;
|
|
|
|
import example.service.impl.ComputerServiceImpl;
|
|
|
|
|
|
|
|
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 java.io.IOException;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
import java.util.List;
|
|
|
|
|
2024-12-21 01:27:15 +08:00
|
|
|
@WebServlet(urlPatterns = {"/computerList","/deleteComputer"})
|
2024-12-21 01:22:44 +08:00
|
|
|
public class ComputerServlet extends HttpServlet {
|
|
|
|
|
|
|
|
IComputerService computerService = new ComputerServiceImpl();
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
|
String path = req.getServletPath();
|
|
|
|
switch (path) {
|
|
|
|
case "/computerList":
|
|
|
|
try {
|
|
|
|
listServlet(req, resp);
|
|
|
|
} catch (SQLException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
break;
|
2024-12-21 01:27:15 +08:00
|
|
|
case "/deleteComputer":
|
|
|
|
deleteComputer(req, resp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void deleteComputer(HttpServletRequest req, HttpServletResponse resp) {
|
|
|
|
String id = req.getParameter("id");
|
|
|
|
try {
|
|
|
|
computerService.deleteComputer(id);
|
|
|
|
resp.sendRedirect("/computerList");
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException(e);
|
2024-12-21 01:22:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void listServlet(HttpServletRequest req, HttpServletResponse resp) throws SQLException, ServletException, IOException {
|
|
|
|
List<Computer> computers= computerService.listComputer();
|
|
|
|
req.setAttribute("computers", computers);
|
|
|
|
req.getRequestDispatcher("computerList.jsp").forward(req, resp);
|
|
|
|
}
|
|
|
|
}
|