This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Servlets and the fly likes Difference between forward and SendRedirect? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Difference between forward and SendRedirect?" Watch "Difference between forward and SendRedirect?" New topic
Author

Difference between forward and SendRedirect?

Vijendra Runwal
Ranch Hand

Joined: Apr 17, 2006
Posts: 52
Can Anyone explain Difference between forward and SendRedirect.

Thanks in Advance.

Vijendra.


SCJP 1.4,SCWCD
nandkishor rao
Ranch Hand

Joined: May 24, 2006
Posts: 53
forward runs on the server side while sendRedirect runs on the client as well as on the server side thats why the response generated by sendRedirect() is slow as compared to rd.forward().By using sendRedirect() you can forward the request to any web application either in the same server or to the another one.Incase of forward() the request has to be forwarded to the same web application.One more difference is that forward() method holds the previous request and response objects while using sendRedirect(),it will create fresh request and response objects.
Sujith Kanaparthi
Ranch Hand

Joined: Sep 04, 2005
Posts: 45
response.sendRedirect() sends a response to the browser asking it to load another page, whereas in the case of RequestDipsatcher.forward() control is transferred to another servlet or jsp within the server.
Using sendRedirect() on one server, we call redirect a call to a resource on located on different server which is not possible using forward().
Hope this may help you.

Regards,
Sujith
Vijendra Runwal
Ranch Hand

Joined: Apr 17, 2006
Posts: 52
A simple example which shows the difference between them will make my concepts about them more clear.
Can anyone list down a example plz.


Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Imagine soccer training (world cup is coming).
PlayerA has a ball. To pass the ball from playerA to playerB, there are two possibilites:
1. PlayerA passes his ball to playerB (forward)
2. PlayerA grabs a brand new ball and sends it to playerB (sendRedirect)

With forward, you are passing the existing request, with everything it already contains (headers, attributes...)
With sendRedirect, you are creating a new request (new headers, no attributes).


[My Blog]
All roads lead to JavaRanch
saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
so new request means it must create new session?
will it creates new session for new request by using sendredirect
:roll:


A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

No, session and requests are different things. Creating a new request doesn't mean creating a new session.
Sujith Kanaparthi
Ranch Hand

Joined: Sep 04, 2005
Posts: 45
As clarified by Satou kurinosuke, session and request are different things.

When forward is used we can pass data to the forwarded page using request.setAttribute() as the same request and response objects available the forwarded servlet/jsp.But in in case of sendRedirect() we have to set the data in session or by appending the data to the URL that will be passed as argument to this method, because by calling this method a new request is created.
Vini Rai
Greenhorn

Joined: May 21, 2006
Posts: 2
Forward request is transparent to the user
Sendredirect is not transparent to the user

if request is sendredirect then its visible in your browser (redirect at client side)

while in case of forward it is done at server side and it is not visible to user.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Difference between forward and SendRedirect?