Sunteți pe pagina 1din 4

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.
PROPERTY INCLUDE() METHOD FORWARD() METHOD

Will forward the client


WHAT CAN BE request to the
DONE? Includes another file in our current file. forwarding page.

Response of S1 and S2 are merged and


sent to client (as if a single response). No merge of response.
MERGE OF This way, the Programmer can achieve Only S2 response will go
RESPONSE "server side includes". to the client.

RETAINING Shifted temporarily from S1 to S2. It


EXECUTION works like a general simple method Shifted permanently
CONTROL call. from S1 to S2.

Execution control comes back to S1


after executing S2 for further
processing of S1 after include() Once shifted, the control
CONTROL statement from where the execution never returns to S1. It is
COMING BACK control shifted. permanent shifting.

RESPONSE Response of S2 is not


PLACEMENT Response of S2 is placed in S1. placed in S2.

CLIENT Response of S1 and S2 is received by Only response of S2 is


RECEIVES client. received by client.

After executing S2,


CONTROL After executing S2, control returned to control returned to
RETURNED S1. client.

Once control is returned to S1 from S2,


any activity can be done on the server Once control returned to
EXTRA like calling another servlet with client, no activity can be
ACTIVITY another RequestDispatcher object. done on S1 or S2.

USAGE Used by Programmer when the output Used only S2 response


of both servlets S1 and S2 is required. is required.

SPEED OF
DELIVERY TO
CLIENT Comparatively slower. Faster.

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 Here also S2 cannot
status code etc. and performing any alter as response is
ACCESS activities like this is simply ignored. delivered on S1 URL.

Used when further


processing responsibility
Often useful just to include another and replying to user is
web resource response like banner given to another Web
USAGE content or copyright information etc. resource.

Output of S1’s
Output of S1’s out.println() statements out.println() statements
OUT.PRINTLN go to client. never go to client.

Client actually receives


the response from a
CLIENT Client receives the response from the different servlet (not
RESPONSE same servlet which he requested. known to client).

TREATMENT The processing S2 can be treated as The processing of S2


OF part of S2 processing. can be treated
PROCESSING completely as a
different entity. It is
used to show a different
resource on the server
by S1.

S-ar putea să vă placă și