109 lines
4.0 KiB
Java
109 lines
4.0 KiB
Java
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;
|
|
|
|
@WebServlet(urlPatterns = {"/computerList","/deleteComputer","/addComputer","/editComputer"})
|
|
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;
|
|
case "/deleteComputer":
|
|
deleteComputer(req, resp);
|
|
break;
|
|
case "/addComputer":
|
|
addComputer(req, resp);
|
|
break;
|
|
case "/editComputer":
|
|
editComputer(req, resp);
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void editComputer(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
String id = req.getParameter("id");
|
|
Computer computer = computerService.getComputer(id);
|
|
req.setAttribute("computer", computer);
|
|
req.getRequestDispatcher("editComputer.jsp").forward(req, resp);
|
|
}
|
|
|
|
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 {
|
|
computerService.deleteComputer(id);
|
|
resp.sendRedirect("/computerList");
|
|
} catch (IOException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
@Override
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
String path = req.getServletPath();
|
|
switch (path) {
|
|
case "/addComputer":
|
|
savaComputer(req, resp);
|
|
break;
|
|
case "/editComputer":
|
|
UpdatComputer(req, resp);
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void UpdatComputer(HttpServletRequest req, HttpServletResponse resp) {
|
|
String id = req.getParameter("id");
|
|
String name = req.getParameter("name");
|
|
String price = req.getParameter("price");
|
|
String stock = req.getParameter("stock");
|
|
computerService.updateComputer(id,name,Double.parseDouble(price),Integer.parseInt(stock));
|
|
try {
|
|
resp.sendRedirect("/computerList");
|
|
} catch (IOException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
void savaComputer(HttpServletRequest req, HttpServletResponse resp) throws 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);
|
|
}
|
|
}
|
|
}
|