HttpServlet, Http 요청 처리하는 DispatchServlet

Ryu·2022년 8월 6일
0

HttpServlet 클래스… ?

🤔 프로젝트를 진행하면서 HttpServlet을 상속하는 DispatchServlet 클래스를 만들어 doGet, doPost 메서드를 오버라이딩해 url 연결에 대해 매핑을 해주었다. 그러나 정작, HttpServlet의 내부 구조와 어떤 동작 원리를 갖고 있는지 궁금해서 찾아보았다.

HttpServlet 클래스

  • 사용자 요청을 처리하는 doGet, doPost 메서드는 모두
    HttpServletRequest , HttpServletResponse 객체를 매개변수로 갖고 있다.
    - 이 두 객체는 서블릿과 클라이언트 사이를 연결해주는 중요한 객체이다.
  • 정확히는 Service 부분에서 담당하는

Servlet 라이프 사이클을 이해하면, 동작 원리를 이해하기 쉽다.

1. 브라우저 요청 -> Servlet 클래스가 로딩, 요청에 대한 Servlet 객체 생성
2. 서버는 init() 메서드를 호출, Servlet 초기화 (서블릿 클래스 로딩 후에는 초기화 작업 없이 바로 호출.)
3. Service() 메서드 호출 => Servlet이 브라우저 요청을 처리하도록 한다.
4. Service() 메서드는 특정 **HTTP 요청을 처리하는 메서드 doGet, doPost 를 호출**합니다.
5. 서버는 destroy() 메서드를 통해 Servlet을 제거한다.

request.getWriter().append();

  • request 객체를 보낸 곳으로 데이터를 전달한다.
    → 쉽게 말하면, request 에 대한 응답이다.
  • append가 아니라 print() 메서드를 사용해도 무방. 단, append는 버퍼에 저장되어 한 번에 출력.

DispatchServlet 생성

DispatchServlet 생성 이유

  • request.getRequestURI(); 로 url 을 받아오고, switch ~ case 분기문으로 각 url 별 Http 요청 처리 메서드를 담기 위해서.
  • 다음은 실제 코드이다.
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Rq rq = new Rq(req, resp);
        MemberController memberController = new MemberController();
        ArticleController articleController = new ArticleController();
    
        // getRequestURI는
        // http://localhost:8081/usr/article/list/free?page=1 에서
        // /usr/article/list/free 부분만 가져온다.
        String url = req.getRequestURI();
    
        switch (url) {
            case "/usr/article/list/free":    
                articleController.showList(rq);
                break;
            case "/usr/member/login":
                memberController.showLogin(rq);
                break;
        }
    }

오답노트

1. break; 문을 빠뜨려서 오류 발생. -> 굉장히 잦은 실수.
2. switch case 분기문에서 url 의 맨 앞 /를 빼먹어서 오류 발생 
  -> req.getRequestURI() 메서드는 /usr/article/list/free 이렇게 가져온다.

📚 개념정리

http://localhost:8080/usr/article/list.jsp
1. request.getContextPath(); => 프로젝트 path 만 가져온다.
  - /project 만 가져온다.
2. request.getRequestURI(); => 프로젝트 + 파일경로까지 가져온다.
  - /usr/article/list.jsp
3. request.getRequestURL(); => 전체 경로를 가져온다.
  - http://localhost:8080/usr/article/list.jsp
4. request.ServletPath(); => 파일명만 가져온다.
  - list.jsp
5. request.getRealPath(); => 절대경로로 가져온다.
  - c:\project\webapp\usr\article
  • 파일만 가져오고 싶다면 ?
    String url =  request.getRequestURI.split("/");
    String fileName = url[url.length-1];   // 마지막 인덱스에 접근, list.jsp 를 가져온다. 
profile
Strengthen the core.

0개의 댓글