개발입문/Servlet&JSP

Request 속성과 Request 디스패칭

haloaround 2017. 2. 16. 00:49

Request Dispatching

이번 한 번의 요청에 대해서만 사용한다면- Request에 속성을 추가하면 됩니다.
(Context, Session 처럼 다른 컴포넌트도 사용할 수 있는 곳에- 장기적으로- 공유하지 않습니다.)

RequestDispatcher는 Request를 전달한 다른 리소스(컴포넌트)를 가리키는 래퍼 클래스를 반환합니다. 이 래퍼클래스로 request, response를 인자로 가지는 forward 메소드를 호출합니다. 이를 통해 실제 response 작업을 RequestDispatcher로 위임합니다.

RequestDispatcher는 아래 기능을 할 래퍼클래스를 지정해준다.

- 다른 서블릿 (다른 서블릿에서 추가작업이 필요한 경우) or
- JSP View (웹페이지 출력용) 

public RequestDispatcher getRequestDispatcher(java.lang.String path)

Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path. A RequestDispatcher object can be used to forward a request to the resource or to include the resource in a response. The resource can be dynamic or static.

http://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/RequestDispatcher.html


[예시코드]

ArrayList<String> result = be.getBrands(c);
request.setAttribute("styles", result);

RequestDispatcher view = request.getRequestDispatcher("result.jsp");
view.forward(request, response);