RequestDispatcher include method

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

RequestDispatcher include

Hello, continuing the previous article, we will learn about RequestDispatcher includemethod.

Why is it needed?

The forward method redirects to another resource which is called through a resource. The response of calling resource is not sent to the client. It is lost. What should be done if we want to retain the response of calling resource and called resource? What if we want to show them both to the client?

How to do it?

RequestDispatcher include method comes to the rescue. This is what javadoc says about RequestDispatcher include .

Includes the content of a resource (servlet, JSP page, HTML file) in the response. In essence, this method enables programmatic server-side includes.

Let us see a practical example of  RequestDispatcher include  method.

Step 1: Create dynamic web project.

Create a new dynamic web project called ServletJSPExamples using eclipse IDE.

Step 2: Create and configure DispatcherServlet.

Create a new servlet called DispatcherServlet which will use RequestDispatcher includemethod to call another resource.

Configure it in web.xml

 Step 3: Create Hello.jsp

 Step 4: Run application.

Deploy the application under tomcat. Start it and hit the url http://localhost:8080/ServletsJSPExamples/DispatcherServlet .

You will see the output as :



  • A PrintWriter is created in DispatcherServlet so that response can be written.
  • After  out.println();   statement, a RequestDispatcher is created for  Hello.jsp   using RequestDispatcher rd = request.getRequestDispatcher("Hello.jsp");
  • rd.include(request, response);  i.e.  RequestDispatcher include  method sends HttpServletRequest and HttpServletResponse objects to the resource for which RequestDispatcher is created. So in this case, it’s Hello.jsp .
  • Control is forwarded to Hello.jsp   and the contents are written to response.
  • Now as we have used  RequestDispatcher include  method, the response from DispatcherServlet is combined with the response of Hello.jsp . It acts as a dynamic include.
  • So both responses are printed onto the web page.

The diagram below will elaborate it clearly.



Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

6 thoughts on “RequestDispatcher include method

  • May 22, 2015 at 3:20 am

    Very nice example and good explanation !! Thanks

  • November 13, 2015 at 11:49 am

    Nice example and clear explanation, Thank you.

  • November 13, 2015 at 11:50 am

    Very clearly explained.Thank you.

  • November 19, 2015 at 2:34 pm

    Thank you! It’s a nice explanation

  • January 26, 2017 at 12:28 am

    This is nice, but there is a problem with the ‘/DispatcherServlet’ URI at the beginning. Normally you don’t want it in your URLs. However, if you map your servlet as /*, you will not be able to use RequestDispatcher properly – accessing resources will result in StackOverflowException because this results in an infinite loop – a servlet requests a JSP, but request delegates the request again back to the servlet because it is mapped to /*. Effectively, RequestDispatcher can never reach a JSP with this design. Am I missing anything or this is a bug in the Servlet/JSP spec?

  • September 4, 2017 at 10:59 am

    A nice and clear explanation.thx


Leave a Reply

Your email address will not be published. Required fields are marked *