16 Differences include vs forward Decide when to use which?

The javax.servlet.RequestDispatcher interface comes with only two methods of include() and forward(). These methods are discussed very clearly with example code, illustrative figures and explanation in RequestDispatcher include Example and RequestDispatcher forward Example. It is advised to go through these two programs before learning the differences.

RequestDispatcher is used whenever the Programmer would like dispatch the request to another resource (like HTML. Servlet, JSP etc.) on the server. Or to say, used to connect to another web resource.

Following figures give the visual difference you can grasp include vs forward.

include vs forward

include vs forward

Let us make a table of differences include vs forward. In the table, I name two Servlets, as seen in the above examples, S1 and S2. S1 is the Servlet which calls S2. Or to say, S1 is including Servlet and S2 is included Servlet.

Property include() Method forward() Method
What can be done? Includes another file in our current file. Will forward the client request to the forwarding page.
Merge of response Response of S1 and S2 are merged and sent to client (as if a single response). This way, the Programmer can achieve "server side includes". No merge of response. Only S2 response will go to the client.
Retaining execution control Shifted temporarily from S1 to S2. It works like a general simple method call. Shifted permanently from S1 to S2.
Control coming back Execution control comes back to S1 after executing S2 for further processing of S1 after include() statement from where the execution control shifted. Once shifted, the control never returns to S1. It is permanent shifting.
Response placement Response of S2 is placed in S1. Response of S2 is not placed in S2.
Client receives Response of S1 and S2 is received by client. Only response of S2 is received by client.
Control returned After executing S2, control returned to S1. After executing S2, control returned to client.
Extra activity Once control is returned to S1 from S2, any activity can be done on the server like calling another servlet with another RequestDispatcher object. Once control returned to client, no activity can be done on S1 or S2.
Usage Used by Programmer when the output of both servlets S1 and S2 is required. Used only S2 response is required.
Speed of delivery to client Comparatively slower. Faster.
Access S2 has access to the request and response objects of S1, but limitations exist. S2 cannot set headers and also cannot call any other method like setCookie etc. affecting the response headers. That is, S2 cannot attempt to change the HTTP headers or response status code etc. and performing any activities like this is simply ignored. Here also S2 cannot alter as response is delivered on S1 URL.
Usage Often useful just to include another web resource response like banner content or copyright information etc. Used when further processing responsibility and replying to user is given to another Web resource.
out.println Output of S1’s out.println() statements go to client. Output of S1’s out.println() statements never go to client.
Client response Client receives the response from the same servlet which he requested. Client actually receives the response from a different servlet (not known to client).
Treatment of processing The processing S2 can be treated as part of S2 processing. The processing of S2 can be treated completely as a different entity. It is used to show a different resource on the server by S1.
Used when Used when static information is to be included. Used when dynamic information is to be included. It is can be used where a Servlet can play the role of a controller to process the client input and deciding what response page is to be returned.

After knowing include vs forward, let us go their similarities.

Similarities:

  1. Both are methods of RequestDispatcher interface.
  2. Both are method calls.
  3. Both are abstract methods in the interface but can be treated as concrete methods in Servlet programming. I mean, because we are not implementing RequestDispatcher to our Servlet program, there is no compulsion to the Programmer to override both.
  4. Processing done on the server when both methods are called is completely transparent (not visible or known) to client.
    Both are not treated as redirection.

4 thoughts on “16 Differences include vs forward Decide when to use which?”

  1. control flow of s2 in the forward() method call is return to s1….. it can’t go directly towards the client
    // in servlet s1
    rd.forward(“./s2”);

    System.out.println(“hello”);

    then check output on console….

Leave a Comment

Your email address will not be published.