Web programming/Patterns
2. FrontControllerServlet ex2
row_a_boat
2021. 4. 18. 17:12
public void handleRequest(request.getParameter("command");
if(command.equals("FindCar")) {
findCar(request, response);
}else if(command.equals("RegisterCar")){
RegisterCar(request, response);
}
}
//차 정보 검색 메서드
public void findCar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
String carNo = request.getParameter("carNo");
String carInfo = MockDAO.getInstance().findCarInfo(carNo);
String view = null;
if(carInfo==null) {
view= "findcar-fail.jsp";
}else {
view="findcar-ok.jsp";
request.setAttribute("carInfo", carInfo);
}
request.getRequestDispatcher(view).forward(request, response);
}
//차 등록 메서드
public void RegisterCar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
String carInfo = request.getParameter("carInfo");
MockDAO.getInstance().registerCar(carInfo);
response.sendRedirect("registercar-result.jsp");
}
//handleRequest 메서드로 hidden값(command)를 받아와서 해당하는 경우에 알맞는 메서드로 command값을 보내준다.
//command가 FindCar과 같다면, findCar메서드에 command값(carNo)를 보내주고,
//command가 RegisterCar와 같다면 registerCar메서드에 command값(carInfo)를 보내준다.