RequestDispatcher forward() Example

RequestDispatcher forward() Example

Communication between the Servlets is an important task to the Programmer. To achieve this, we studied reading private data of a servlet and reading global data by all servlets. Now let us see how to pass data between two servlets (one-to-one) and for this Servlet API comes with javax.servlet.RequestDispatcher interface.

When to use RequestDispatcher interface?

RequestDispatcher is used in two cases.

  1. To include the response (output) of one Servlet into another (that is, client gets the response of both Servlets).
  2. To forward the client request to another Servlet to honour (that is, client calls a Servlet but response to client is given by another Servlet).

How many methods exist in RequestDispatcher interface?

Only two methods exist: RequestDispatcher include() and RequestDispatcher forward(). Let us see what Servlet API says about these methods.

  • void include(ServletRequest request, ServletResponse response) throws ServletException, IOException: Includes the content of a resource (Servlet, JSP page, HTML file) in the response. In essence, this method enables programmatic server-side includes.
  • void forward(ServletRequest request, ServletResponse response) throws ServletException, IOException: Forwards a request from a Servlet to another resource (Servlet, JSP file, or HTML file) on the server. This method allows one Servlet to do preliminary processing of a request and another resource to generate the response.

The include() method is covered in RequestDispatcher include Example with realtime scenarios, coding examples and explanation in length. Now let us see about forward() method right now.

How to obtain an object of RequestDispatcher interface?

For this two styles exist – using ServletRequest object and ServletContext object. getRequestDispatcher(String path) method returns an object of RequestDispatcher and the same method exist in both interfaces of ServletRequest and ServletContext. Small difference in the usage exist between the two and we see later. In this posting, ServletRequest is used.

Let us see what API says about getRequestDispatcher(String path) method:

  • RequestDispatcher getRequestDispatcher(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.

RequestDispatcher forward() Method

When to use RequestDispatcher forward() method?

Client calls a Servlet for some information. But the Servlet cannot honour the request because it is incapable. But it knows that another Servlet exists which can do the job of the client. Then how the first Servlet (called by the client) can send (forward) the request to another Servlet. Here, forward() method of RequestDispatcher is used.

That is, client calls one Servlet (say S1) but response goes from another Servlet (say S2). Ofcourse, the client is not aware of all this affairs happening on the server.

Here, response of S2 goes to client but never of S1. Remember, in include() method both S1 and S2 goes.

Some realtime scenarios to use RequestDispatcher forward() method.

  1. USA Population: I call a servlet on an Indian server (which can give India population) to fetch me the population of USA. But the Servlet on the Indian server cannot but it knows the address of a Servlet on the USA server that can fulfill the client request. Instead of telling sorry, the Indian Servlet can send a message to USA Servlet to honour the client request with forward() method.
  2. Order Format: The client sends an order to a company for some goods by calling a Servlet (say S1). But it is not in the format required by the Sales department. S1 Servlet would like to forward the client request to another Servlet (say S2) that can convert the client data into the correct format. To pass the client request to S2, S1 uses forward() method.
Example on RequestDispatcher forward() method

In the following example code, client sends two numbers to a Servlet to know their product. It calls a Servlet GetMe (with alias name S1 in web.xml). S1 forwards the client request to Product Servlet (of alias name S2) using forward() method of RequestDispatcher interface.

Client Program: TwoNumbers.html

S1 is the alias name of GetMe Servlet which the client calls.

Following is the web.xml entry for the two Servlets.

Now let us write the GetMe servlet.

The First servlet – File Name: (alias name S1 in web.xml)

The Second servlet – File Name: (alias name S2)

Now what client will receive and will not receive:

That is, response of S1 never goes to the client and only S2 goes (eventhough Hello 1 exists before calling forward() method in GetMe Servlet). Following figure explains.

RequestDispatcher forward()

Observe the figure. Client request (or calls) the Servlet S1. S1 sends client data to S2 with forward() method. Response of S2 goes directly to client.

Let us see the screens.

Client Program: HTML file, TwoNumbers.html, when fields are filled up and clicked submit button.

RequestDispatcher forward()

Output screen.

RequestDispatcher forward()

Observe the output. The output (response) of S2 (Product Servlet) only goes to client but not of S1 (GetMe Servlet). Hello 1 and Hello 2 of S1 are not displayed in the output screen.

9 thoughts on “RequestDispatcher forward() Example

  1. Nitu Sharma

    “JSPs are more useful for generating output. Justify with usage of requestDispatcher() and appropriate example”. I want this question’s answer.. can u explain me this answer..

  2. zaheer

    “Some realtime scenarios to use include() method” In this line it should not be include() instead it should be forward()

Leave a Reply

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