What is RequestDispatcher?

In this post, you will learn:

  • What is RequestDispatcher?
  • Why is RequestDispatcher needed?
  • What are the models of processing a request in a web application?

Before we move on the the RequestDispatcher, lets take an analogy to understand request processing. A servlet as you know is a web application component which processes request and generates dynamic contents. In real life any party which does the same can be taken as servlet. In Tech Mentro, we have receptionists who receive customer enquiries and respond to them. Their response differs from enquiry to enquiry because each customer asks different questions hence they can be assumed to be servlet for the sake of analogy. All the customer enquires can be divided into following three categories on the basis how they are resolved.

1. The customer asks normal questions such as what courses do you offer, what are their fee, how and when are classes conducted etc. Receptionists know the answer of all these questions and respond on their own.

2. The customer in addition to the normal questions, have some technical queries such as which version of Hibernate do you cover or which implementation of JAX-WS do you cover etc. The Receptionist doesn’t know the answer of these questions but respond to them by taking the help of some technical person.

3. The customer asks the receptionist to connect him to the trainer so that he can have some technical discussion with him. In this case the receptionist simply forwards the call to the requested person who responds to the customer queries.

A web application like our institute, has servlets (receptionists) who receive requests (customer enquiries) and respond to them in the three ways discussed above i.e. in a web application, a request can be processed in the following three ways:

request processing model1

1.0 – Client sends a request to a Servlet.

1.1 – The Servlet processes the request on its own and generates response.

request processing model 2

1.0 – Client sends a request to a Servlet.

1.1 – The Servlet processes the request and includes the contents of some other servlet or HTML page to its own response.

1.2 – Combined response is sent by the servlet as processing result to the client.

request processning model 3

1.0 – Client sends a request to a Servlet.

1.1 – The Servlet does pre-processing and forwards the request to another servlet or HTML page.

1.2 – Response for the client is generated by the forwarded component.

In order to include the contents of another component or to forward the request to another component from a servlet we need RequestDispatcher. RequestDispatcher is a Servlet API component which provides the functionality of inclusion and forwarding. It is an interface which is implemented by server vendors. It provides following methods:

include(): this method is used by a servlet to include the contents of another servlet or HTML page to its response. It has following syntax:

public void include(ServletRequest request, ServletResponse response) throws ServletException, IOException;

forward(): this method is used by a servlet to forward the request to another servlet or HTML page. It has following syntax:

public void forward(ServletRequest request, ServletResponse response) throws ServletException, IOException;

In these methods, The component to which request is forwarded or contents of which are included, is not specified because it is specified at the time of creating the RequestDispatcher object. A RequestDispatcher object can be created in more than one ways. The simple approach is to get it created through the getRequestDispatcher() method of ServletRequest interface. It has following syntax:

public RequestDispatcher getRequestDispatcher(String componentPath);

Other ways of creating a RequestDispatcher object will be discussed in later posts when concepts related to them would have been fully explained.


Its time to recap the main concepts:

  1. A request can be processed in an application in 3 ways which correspond to the ways we do things in real life.
  2. A Servlet can include the contents of another component to its response.
  3. A Servlet can forward the request to another component.
  4. For forwarding and inclusion, RequestDispatcher is required.
  5. ServletRequest interface provides getRequestDispatcher() method for obtaining a RequestDispatcher object.
If you like the post, then share it...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Leave a Reply

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