# 프론트 컨트롤러 도입 - v1

![v1 구조](/files/-Me-03EOazLAMjklGEES)

**객체 지향 개념 중 다형성을 기반으로 ControllerV1을 인터페이스로 구현하고, 각각의 기능을 하는 컨트롤러는 상속받도록 한다!**

**그러면 프론트 컨트롤러는 이 인터페이스를 호출해서 구현과 관계없이 로직의 일관성을 가져갈 수 있다!**

**정말 중요**⭐️❤️

1. **URL 맵핑 정보는 HashMap으로 만든다.**\
   **key : 요청 URL, value : 호출할 컨트롤러**
2. **request.getRequestURI()로 요청 URL 정보를 가져올 수 있다. 이값이 Map의 key가 되기 때문에 key로 value(호출할 컨트롤러)를 찾는다!**
3. **2번의 value는 key에 따라 MemberFormController, MemberSaveController, MemberListController일 수 있다. 이 3가지는 모두 ControllerV1 인터페이스를 상속받은 구현체들이기 때문에 key를 인터페이스로 담는다!**
4. **key에 따른 value가 null이 아니면 key에 해당하는 controller를 실행한다!**\
   **인터페이스 Controller의 process 메서드가 있다. 구현체들은 이 메서드들을 똑같이 상속받고, 오버라이딩했으므로, 해당하는 controller에 따라 다른 메서드가 실행된다!**

```java
package hello.servlet.web.frontcontroller.v1;

import hello.servlet.web.frontcontroller.v1.controller.MemberFormControllerV1;
import hello.servlet.web.frontcontroller.v1.controller.MemberListControllerV1;
import hello.servlet.web.frontcontroller.v1.controller.MemberSaveControllerV1;

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.util.HashMap;
import java.util.Map;

@WebServlet(name="frontControllerServletV1",urlPatterns = "/front-controller/v1/*")
public class FrontControllerServletV1 extends HttpServlet {
    //맵핑
    private Map<String,ControllerV1> controllerMap = new HashMap<>();

    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 req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("FrontControllerServletV1.service");

        String requestURI = req.getRequestURI();// 이것은 /front-controller/v1/members/new-form와 같다
        //요청 URI가 key가 되고 우리가 정의한 URI라면 맵핑했던 value(웹페이지)를 리턴한다!
        //다형성으로 인해 부모인 ControllerV1으로 받을 수 있다!
        //왜냐하면 아래 Controller변수인 controller는 사실 MemberSaveControllerV1 또는 MemberListControllerV1이기 때문이다!
        ControllerV1 controller = controllerMap.get(requestURI);
        if(controller == null){
            resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        //잘 조회됐다면 컨트롤러 인터페이스의 process 메서드 실행한다!
        //다형성에 의해 오버라이드된 메서드가 호출된다! 소름~! 그래서 같은 process 이름으로 메서드를 호출했구나! 대박
        controller.process(req,resp);
    }
}

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://heunnajo.gitbook.io/mvc/4.-mvc/v1.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
