request
์ response
๊ฐ์ฒด๋ฅผ ๊ฐ๋ ํ ๊ฐ ๋ฉ์๋๋ฅผ ๊ฐ๋๋ค.public interface ControllerV1 {
void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, 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);
}
}
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);
String viewPath = "/WEB-INF/views/save-result.jsp";
RequestDispatcher dispatcher = request.getRequestDispatcher(viewPath);
request.setAttribute("member",member);
dispatcher.forward(request, response);
}
}
public class MemberListControllerV1 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);
}
}
Map
์ ์ด์ฉํ์ฌ URI์ Controller์ ๊ตฌํ์ฒด๋ฅผ ๋ฏธ๋ฆฌ ์ ์ฅํฉ๋๋ค.request.getRequestURI()
๋ฉ์๋๋ฅผ ํตํด ์์ฒญ๋ URI๊ฐ์ ํ์ฑํฉ๋๋ค.localhost:8080/test/members
๊ฐ ์๋ค๋ฉด /test/member
๋ถ๋ถ์
๋๋ค.process()
๋ฉ์๋๋ฅผ ํธ์ถํฉ๋๋ค.@WebServlet(name = "frontControllerServletV1", urlPatterns = "/front-controller/v1/*")
public class FrontControllerServletV1 extends HttpServlet {
// URI ๋งคํ์ ๋ณด์ Controller์ ๊ตฌํ์ฒด๋ฅผ ์ ์ฅ
private Map<String, ControllerV1> controllerMap = new HashMap<>();
// URI์ ๋ฐ๋ผ Controller์ ๊ตฌํ์ฒด๋ฅผ ๋ฏธ๋ฆฌ ์์ฑ
public FrontControllerServletV1() {
controllerMap.put("/front-controller/v1/members/new-form", new MemberFormControllerV1());
controllerMap.put("/front-controller/v1/members/save", new MemberSaveControllerV1());
controllerMap.put("/front-controller/v1/members", new MemberListControllerV1());
}
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// URI ๋งคํ์ ๋ณด ํ์ฑ
String requestURI = request.getRequestURI();
// ํ์ฑ๋ URI์ ์ ํฉํ ๊ตฌํ์ฒด๋ฅผ get
ControllerV1 controller = controllerMap.get(requestURI);
if (controller == null) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
// ์ ํฉํ ๊ตฌํ์ฒด์ ๋ฉ์๋ ํธ์ถ
controller.process(request, response);
}
}
์ธํ๋ฐ ๊น์ํ๋์ ์คํ๋ง MVC 1ํธ ์ ์๊ฐํ๊ณ ์ ๋ฆฌํ ๋ด์ฉ์ ๋๋ค.