Difference between Include, Forward and sendRedirect in
Servlet?

Answer Posted / suresh d.v

Before explaining the difference between include,forward and sendRedirect a small information on the request and response objects which is created by servlet container.

Whenever there is request from the client (i.e is browser eg: Internet explorer,safari,netscape etc., ) to the server, the servlet container creates two objects

1. request 2. response these objects are created by servlet container.

Now coming to our differences:-

all the three are used for inter servlet communication in different ways.

Forward :

Consider there are 2 servlets, ser1 and ser2, and now you are calling the ser2 from ser1, as we already discuss every servlet will be having req,res objects.
So ser1 contains req,res objects and here the same objects will be forwarded to ser2, with out knowledge of the container the request will be forwarded to ser2, and in this case for 2 servlets only one request and repsone objects will be there.

Cautions : Forward need to be call before response is committed to the client otherwise you will end up with and exception.

limitation : You can't forward the request outside of the context so the request should with in your site.

Adv : performance wise it will be bit faster as second call is made with out knowledge of the container and there will not be a round trip to the client to server.

sendRedirect : Here also consider same example ser1 and ser2 and now if you want to call ser2 from ser1, in this case the request and response of the ser1 will be discarded and u will be redirected to the client, once again the new request will be made from client for ser2 and container will create new req,and resp objects for ser2.

So if you see here in this case 2 request and 2 response objects are created even though first request,response objects are discarded container will create total 4 objects (2 req, 2resp) and there is a round trip to the server.

Adv: You can access the servlet which is out side of the context also, so u can make a call to the servlet which is there in the other web site.

lim: There is a round trip performance wise bit slow and as there is no req,ans resp objects of serv1 u can't get the values of ser1 and ser2.


Include : Include and forward are almost similar the only difference is, in include the ser2 will be embedded in ser1 and literally there will be only servlet will be there as it includes the the ser2, where as in forward it will invoke the second servlet dynamically so it won't embed the ser2 in ser1.

So one more major diff is as the include will embed the contents into ser2 no need to take care of response committed to client.

I know the post bit lengthy the reason is i tried to explain everything clearly.

Is This Answer Correct ?    72 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

When a servlet accepts a call from a client, it receives two objects. What are they?

771


Can we override destroy method in servlet?

506


How are filters?

585


What are different Authentication options available in Servlets.

569


What is called servlet container?

667






Explain the methods of request dispatcher in servlet?

517


Explain Action Servlet?

625


How will two or three servlets interact or communicate with each other?

916


What do you mean by web applications? Explain web application directory arrangement?

542


explain the advantages of servlet life cycle?

562


What do you mean by httpservlet?

568


What is servlet and its types?

557


What is cookie in servlet?

597


Explain get and post.

649


Explain the features are in servlet 3?

603