관리 메뉴

KorSA

Servlet Container, Servlet, 그리고 RequestDispatcher 본문

Study Memos/Spring Framework

Servlet Container, Servlet, 그리고 RequestDispatcher

Praiv. 2019. 1. 17. 03:51
320x100


[ 사용자의 요청이 처리되는 과정 ]

위 그림을 보면,


1. Servlet Container 는 Client 로부터 Http Request 가 오면 HttpServletRequest, HttpServletResponse 두 객체를 생성


2. Client 가 요청한 내용에 해당하는 Servlet의 service() 를 호출


3. 결과를 HttpServletResponse 에 실어서 Client 에게 보냄


4. Servlet Container 는 응답이 완료되면 HttpServletRequest, HttpServletResponse 두 객체를 소멸



[ RequestDispatcher ]

- Client 로부터 오는 요청(Request) 을 받고, 적절한 응답(Response) 을  Client에게 돌려주는 역할을 하는 객체.

Servlet Container는 RequsetDispatcher object를 생성하여 가지고 있다. RequestDispatcher는 JSP 파일, Servlet, HTML 파일 등 서버측의 리소스를 wrapping 하여 Client 에게 제공한다.


- 작업을 다른 Servlet 이나 JSP로 위임할 때 사용된다. 



[ 페이지 전환 ]

페이지를 전환하기 위한 2가지 방법으로 RequestDispatcher 클래스를 사용하는 방식과, HttpServletResponse.SendRedirect() 클래스를 사용하는 방식이 있다. 우선 큰 차이점은 RequestDispatcher 의 경우 파라미터의 정보를 유지한 채 페이지를 전환할 수 있다는 점이다. HttpServletResponse는 유지되지 않는다. 말 그대로 redirect 하기 때문이다.


- RequestDispatcher를 좀 더 깊게 보면, RequestDispatcher.forward() 와 RequestDispatcher.include() 두 가지 방식 중 하나를 사용한다.

forward() 의 경우 작업을 한 번 위임하면 다시 이전 Servlet 으로 제어권이 돌아오지 않는다. 에러 페이지가 그 예이다.

include() 의 경우 작업을 위임해도 다시 이전 Servlet 으로 제어권이 돌아올 수 있다. 그래서 한 JSP 에서 여러 페이지를 포함할 떄 사용할 수 있다.



참고자료

[서블릿/JSP] RequestDispatcher란. RequestDispatcher로 forward() 하기

(https://dololak.tistory.com/502)


RequestDispatcher, 포워딩( forwarding ), 인클루딩( including )

(http://cocoballl.blogspot.com/2014/07/requestdispatcher-fowarding-including.html)


RequestDispatcher 이란 ?

(http://themangs.tistory.com/entry/RequestDispatcher-%EC%9D%B4%EB%9E%80)


Dispatcher 방식과 Redirect 방식

(http://devbox.tistory.com/entry/Comporison-Dispatcher%EB%B0%A9%EC%8B%9D%EA%B3%BC-Redirect-%EB%B0%A9%EC%8B%9D)


서블릿과 서블릿 컨테이너란 무엇인가 ?

(https://minwan1.github.io/2017/10/08/2017-10-08-Spring-Container,Servlet-Container/)

728x90
728x90

'Study Memos > Spring Framework' 카테고리의 다른 글

Spring Security 메모  (0) 2021.06.07
Java Coding Convention  (0) 2021.05.18
스프링 기본  (0) 2021.03.22
Spring Framework 공부한 내용 정리  (0) 2019.01.15
Comments