41 lines
1.4 KiB
Java
41 lines
1.4 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"})
|
||
|
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;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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);
|
||
|
}
|
||
|
}
|