Difference between Include, Forward and sendRedirect in
Servlet?

Answers were Sorted based on User's Feedback



Difference between Include, Forward and sendRedirect in Servlet?..

Answer / md.farookh.mb

Response.sendRedirect () This function is used, when we
want to redirect
the client request to some other site (i.e out of our
context) or
when ever we want to redirect errors. If you are using
sendRedirect (), then it will be visible to the client that
means the URL which you have been redirected will be
visible in the address bar. Redirect response to the client
using the specified redirect location URL.

RequestDispatcher Interface:
Forward() : This can be done in two ways by Request &
ServeletContext.
Forwarding a request from a servlet to another resource
(servlet, JSP
file, or HTML file) on the server. Forward is done at
server side, without the client's knowledge.

When you invoke a forward request, the request is sent to
another
resource on the server, without the client being informed
that a
different resource is going to process the request. This
process occurs
completely with in the web container. When a sendRedirtect
method is
invoked, it causes the web container to return to the
browser indicating
that a new URL should be requested. Because the browser
issues a
completely new request any object that are stored as
request attributes
before the redirect occurs will be lost. This extra round
trip a
redirect is slower than forward.
Include:Includes dynamic page information..

Is This Answer Correct ?    130 Yes 15 No

Difference between Include, Forward and sendRedirect in Servlet?..

Answer / abilash

include: will include another file in our current file

forward: will forward the current request to the forwarding page

sendRediect: will call the next page but it won't send the
same request to the new page but forward will send the same
request to the new page

Is This Answer Correct ?    96 Yes 13 No

Difference between Include, Forward and sendRedirect in Servlet?..

Answer / 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

Difference between Include, Forward and sendRedirect in Servlet?..

Answer / franklin

Include:It helps us to include the contents of some other
file in a JSP

jsp:include-it includes the contents during the run time

forward:will send the same request to the new server.Client
will not know that he s redirected to the new page as
forwarding will take place at the server end.

sendRedirect:will not send the same request to the
server.Client will know that he s redirected to the other
site.

Is This Answer Correct ?    27 Yes 17 No

Difference between Include, Forward and sendRedirect in Servlet?..

Answer / samit katiyar

forward() should be called before the response has been
committed to the client (before response body output has
been flushed). If the response already has been committed,
this method throws an IllegalStateException. Uncommitted
output in the response buffer is automatically cleared
before the forward.

While with include()...... The request and response
parameters must be either the same objects as were passed
to the calling servlet's service method or be subclasses of
the ServletRequestWrapper or ServletResponseWrapper classes
that wrap them.

Is This Answer Correct ?    8 Yes 3 No

Difference between Include, Forward and sendRedirect in Servlet?..

Answer / shyam babu

include: will include the output of another resource(jsp,
servlet etc )in our current file. and response is generated
from current file

forward: will forward the current request to the forwarding
page & response is generated from forwarding opage

sendRediect: will redirect to the page specified in the
sendRedirect method but it won't send the
same request to the new page but forward will send the same
request to the new page

Is This Answer Correct ?    8 Yes 7 No

Difference between Include, Forward and sendRedirect in Servlet?..

Answer / saikiran

main difference between include forwared is
pathelements:
-------------
pathelements are not available to the sended servlet
that are to be available to the other servlet in the c
ase of forward.
incase of include both pathelements are available to
the both servlets.
Response Generation:
--------------------
in the response generation forward forwarded servlet
message will be printed
eg:
two servlets ser1 and ser2
ser1 forwarded to ser2 then ser2 messages will be
displayed not by ser1
in case of include both of them

Is This Answer Correct ?    8 Yes 11 No

Post New Answer

More Servlets Interview Questions

What is load-on-startup in servlet?

0 Answers  


Explain the features are in servlet 3?

0 Answers  


Which event is fired at the time of session creation and destroy?

0 Answers  


What are different ways for servlet authentication?

0 Answers  


What are the types of servlets? Explain

0 Answers  






What do you understand by mime type?

0 Answers  


What are setSecure() and getSecure() methods in Cookies?

1 Answers  


What are the kinds of http requests?

0 Answers  


Why should we go for interservlet communication?

2 Answers  


Differentiate between the web server and application server?

0 Answers  


What is the servletconfig object?

0 Answers  


what is SSL?

3 Answers  


Categories