목차
프론트 컨트롤러 패턴
프론트 컨트롤러 도입 전

프론트 컨트롤러 도입 후

V1 구조

FrontController 패턴 특징
- 컨트롤러 서블릿 하나로 클라이언트 요청 처리(프론트 컨트롤러가 요청에 맞는 컨트롤러를 찾아서 호출)
- 입구가 하나 (공통 처리가 가능)
- 나머지 컨트롤러는 서블릿을 사용하지 않아도 된다. (중복 제거)
ControllerV1
package hello.servlet.web.frontcontroller.v1;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
public interface ControllerV1 {
void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;
}
- servlet과 비슷한 컨트롤러 인터페이스 구현
- 다형성을 활용해 여러 인터페이스를 구현하기 위해서
- 사용자 입력 기능(form), 저장 기능(save), 조회 기능(list)
MemberFormControllerV1
package hello.servlet.web.frontcontroller.v1.controller;
import hello.servlet.web.frontcontroller.v1.ControllerV1;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
public class MemberFormControllerV1 implements ControllerV1 {
@Override
public void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String viewPath = "/WEB-INF/views/new-form.jsp";
RequestDispatcher dispatcher = request.getRequestDispatcher(viewPath);
dispatcher.forward(request,response);
}
}
MemberSaveControllerV1
package hello.servlet.web.frontcontroller.v1.controller;
import hello.servlet.domain.member.Member;
import hello.servlet.domain.member.MemberRepository;
import hello.servlet.web.frontcontroller.v1.ControllerV1;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
public class MemberSaveControllerV1 implements ControllerV1 {
private MemberRepository memberRepository = MemberRepository.getInstance();
@Override
public void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
int age = Integer.parseInt(request.getParameter("age"));
Member member = new Member(username, age);
memberRepository.save(member);
// Model에 데이터 저장
request.setAttribute("member",member);
String viewPath = "/WEB-INF/views/save-result.jsp";
RequestDispatcher dispatcher = request.getRequestDispatcher(viewPath);
dispatcher.forward(request, response);
}
}
MemberListControllerV1
package hello.servlet.web.frontcontroller.v1.controller;
import hello.servlet.domain.member.Member;
import hello.servlet.domain.member.MemberRepository;
import hello.servlet.web.frontcontroller.v1.ControllerV1;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
public class MemberListController implements ControllerV1 {
private MemberRepository memberRepository = MemberRepository.getInstance();
@Override
public void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Member> members = memberRepository.findAll();
request.setAttribute("members",members);
String viewPath = "/WEB-INF/views/members.jsp";
RequestDispatcher dispatcher = request.getRequestDispatcher(viewPath);
dispatcher.forward(request, response);
}
}
FrontControllerServlet
package hello.servlet.web.frontcontroller.v1;
import hello.servlet.web.frontcontroller.v1.controller.MemberFormControllerV1;
import hello.servlet.web.frontcontroller.v1.controller.MemberListController;
import hello.servlet.web.frontcontroller.v1.controller.MemberSaveControllerV1;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@WebServlet(name = "frontControllerServletV1", urlPatterns = "/front-controller/v1/*")
public class FrontControllerServletV1 extends HttpServlet {
private Map<String, ControllerV1> controllerV1Map = new HashMap<>();
public FrontControllerServletV1(){
controllerV1Map.put("/front-controller/v1/members/new-form",new MemberFormControllerV1());
controllerV1Map.put("/front-controller/v1/members/save",new MemberSaveControllerV1());
controllerV1Map.put("/front-controller/v1/members",new MemberListController());
}
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String requestURI = request.getRequestURI();
ControllerV1 controller = controllerV1Map.get(requestURI);
if (controller == null){
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
controller.process(request,response);
}
}
urlPatterns
urlPatterns = "/front-controller/v1/*"
/front-controller/v1
포함한 하위 모든 요청은 이 서블릿에서 받아들인다.
controllerMap
- key: 매핑 URL
- value: 호출될 컨트롤러
service()
requestURI
를 조회해서 실제 호출할 컨트롤러를controllerMap
에서 찾는다.- 만약 없다면 404(SC_NOT_FOUND) 상태 코드를 반환(예외 처리)
- 컨트롤러를 찾고
controller.process(request, response);
을 호출해서 해당 컨트롤러를 실행한다.
JSP
JSP는 이전 MVC에서 사용했던 것을 그대로 사용한다.
'Java > spring' 카테고리의 다른 글
[Java][Spring] 서블릿 종속성 제거 -V3 (0) | 2024.01.04 |
---|---|
[Java][Spring] View 분리 -V2 (0) | 2024.01.04 |
[Java][Spring] MVC 패턴의 한계 (1) | 2024.01.03 |
[Java][Spring] MVC 패턴 적용 (0) | 2023.12.29 |
[Java][Spring] JSP로 회원 관리 웹 애플리케이션 만들기 (1) | 2023.12.29 |
프론트 컨트롤러 패턴
프론트 컨트롤러 도입 전

프론트 컨트롤러 도입 후

V1 구조

FrontController 패턴 특징
- 컨트롤러 서블릿 하나로 클라이언트 요청 처리(프론트 컨트롤러가 요청에 맞는 컨트롤러를 찾아서 호출)
- 입구가 하나 (공통 처리가 가능)
- 나머지 컨트롤러는 서블릿을 사용하지 않아도 된다. (중복 제거)
ControllerV1
package hello.servlet.web.frontcontroller.v1;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
public interface ControllerV1 {
void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;
}
- servlet과 비슷한 컨트롤러 인터페이스 구현
- 다형성을 활용해 여러 인터페이스를 구현하기 위해서
- 사용자 입력 기능(form), 저장 기능(save), 조회 기능(list)
MemberFormControllerV1
package hello.servlet.web.frontcontroller.v1.controller;
import hello.servlet.web.frontcontroller.v1.ControllerV1;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
public class MemberFormControllerV1 implements ControllerV1 {
@Override
public void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String viewPath = "/WEB-INF/views/new-form.jsp";
RequestDispatcher dispatcher = request.getRequestDispatcher(viewPath);
dispatcher.forward(request,response);
}
}
MemberSaveControllerV1
package hello.servlet.web.frontcontroller.v1.controller;
import hello.servlet.domain.member.Member;
import hello.servlet.domain.member.MemberRepository;
import hello.servlet.web.frontcontroller.v1.ControllerV1;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
public class MemberSaveControllerV1 implements ControllerV1 {
private MemberRepository memberRepository = MemberRepository.getInstance();
@Override
public void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
int age = Integer.parseInt(request.getParameter("age"));
Member member = new Member(username, age);
memberRepository.save(member);
// Model에 데이터 저장
request.setAttribute("member",member);
String viewPath = "/WEB-INF/views/save-result.jsp";
RequestDispatcher dispatcher = request.getRequestDispatcher(viewPath);
dispatcher.forward(request, response);
}
}
MemberListControllerV1
package hello.servlet.web.frontcontroller.v1.controller;
import hello.servlet.domain.member.Member;
import hello.servlet.domain.member.MemberRepository;
import hello.servlet.web.frontcontroller.v1.ControllerV1;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
public class MemberListController implements ControllerV1 {
private MemberRepository memberRepository = MemberRepository.getInstance();
@Override
public void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Member> members = memberRepository.findAll();
request.setAttribute("members",members);
String viewPath = "/WEB-INF/views/members.jsp";
RequestDispatcher dispatcher = request.getRequestDispatcher(viewPath);
dispatcher.forward(request, response);
}
}
FrontControllerServlet
package hello.servlet.web.frontcontroller.v1;
import hello.servlet.web.frontcontroller.v1.controller.MemberFormControllerV1;
import hello.servlet.web.frontcontroller.v1.controller.MemberListController;
import hello.servlet.web.frontcontroller.v1.controller.MemberSaveControllerV1;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@WebServlet(name = "frontControllerServletV1", urlPatterns = "/front-controller/v1/*")
public class FrontControllerServletV1 extends HttpServlet {
private Map<String, ControllerV1> controllerV1Map = new HashMap<>();
public FrontControllerServletV1(){
controllerV1Map.put("/front-controller/v1/members/new-form",new MemberFormControllerV1());
controllerV1Map.put("/front-controller/v1/members/save",new MemberSaveControllerV1());
controllerV1Map.put("/front-controller/v1/members",new MemberListController());
}
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String requestURI = request.getRequestURI();
ControllerV1 controller = controllerV1Map.get(requestURI);
if (controller == null){
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
controller.process(request,response);
}
}
urlPatterns
urlPatterns = "/front-controller/v1/*"
/front-controller/v1
포함한 하위 모든 요청은 이 서블릿에서 받아들인다.
controllerMap
- key: 매핑 URL
- value: 호출될 컨트롤러
service()
requestURI
를 조회해서 실제 호출할 컨트롤러를controllerMap
에서 찾는다.- 만약 없다면 404(SC_NOT_FOUND) 상태 코드를 반환(예외 처리)
- 컨트롤러를 찾고
controller.process(request, response);
을 호출해서 해당 컨트롤러를 실행한다.
JSP
JSP는 이전 MVC에서 사용했던 것을 그대로 사용한다.
'Java > spring' 카테고리의 다른 글
[Java][Spring] 서블릿 종속성 제거 -V3 (0) | 2024.01.04 |
---|---|
[Java][Spring] View 분리 -V2 (0) | 2024.01.04 |
[Java][Spring] MVC 패턴의 한계 (1) | 2024.01.03 |
[Java][Spring] MVC 패턴 적용 (0) | 2023.12.29 |
[Java][Spring] JSP로 회원 관리 웹 애플리케이션 만들기 (1) | 2023.12.29 |