반응형
📌컨트롤러 클래스
MemberFrontController.java
앞서 설명했듯이 모든 클라이언트의 요청을 한 컨트롤러 파일이 받아서 제어하는 FrontController 패턴을 구현할 것이다.
package controller;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import action.Action;
import action.MemberDeleteAction;
import action.MemberJoinAction;
import action.MemberListAction;
import action.MemberLoginAction;
import action.MemberViewAction;
import vo.ActionForward;
@WebServlet("*.me")
public class MemberFrontController extends javax.servlet.http.HttpServlet
{
static final long serialVersionUID = 1L;
protected void doProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String RequestURI=request.getRequestURI();
String contextPath=request.getContextPath();
String command=RequestURI.substring(contextPath.length());
ActionForward forward=null;
Action action=null;
if(command.equals("/memberLogin.me")){
forward=new ActionForward();
forward.setRedirect(true);
forward.setPath("./loginForm.jsp");
}else if(command.equals("/memberJoin.me")){
forward=new ActionForward();
forward.setRedirect(false);
forward.setPath("./joinForm.jsp");
}else if(command.equals("/memberLoginAction.me")){
action = new MemberLoginAction();
try{
forward=action.execute(request, response);
}catch(Exception e){
e.printStackTrace();
}
}else if(command.equals("/memberJoinAction.me")){
action = new MemberJoinAction();
try{
forward=action.execute(request, response);
}catch(Exception e){
e.printStackTrace();
}
}else if(command.equals("/memberListAction.me")){
action = new MemberListAction();
try{
forward=action.execute(request, response);
}catch(Exception e){
e.printStackTrace();
}
}else if(command.equals("/memberViewAction.me")){
action = new MemberViewAction();
try{
forward=action.execute(request, response);
}catch(Exception e){
e.printStackTrace();
}
}else if(command.equals("/memberDeleteAction.me")){
action = new MemberDeleteAction();
try{
forward=action.execute(request, response);
}catch(Exception e){
e.printStackTrace();
}
}
if(forward != null){
if(forward.isRedirect()){
response.sendRedirect(forward.getPath());
}else{
RequestDispatcher dispatcher=
request.getRequestDispatcher(forward.getPath());
dispatcher.forward(request, response);
}
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doProcess(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
doProcess(request,response);
}
}
클라이언트에서 넘어오는 요청이 GET 방식이든 POST 방식이든 모든 요청이 doProcess 메소드를 호출하는 구조이다. 즉 모든 요청을 처리하는 로직이 정의되어 메소드가 doProcess이다.
추가로 URL 경로를 얻어오는 부분은 다음 링크를 참고하면 된다.
https://yeo-computerclass.tistory.com/228
반응형
'JSP & Servlet > 실습' 카테고리의 다른 글
[JSP & Servlet] 회원 관리 - Service / DAO (0) | 2022.07.03 |
---|---|
[JSP & Servlet] 회원 관리 - Action (0) | 2022.07.03 |
[JSP & Servlet] 회원 관리 - 테이블 생성 및 vo(Value Object) + 기본 세팅 (0) | 2022.07.02 |
[JSP & Servlet] 쇼핑몰 (오늘 본 상품 & 장바구니) - View (0) | 2022.07.02 |
[JSP & Servlet] 쇼핑몰 (오늘 본 상품 & 장바구니) - DAO (0) | 2022.07.01 |