• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between forward and SendRedirect?

 
Vijendra Runwal
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can Anyone explain Difference between forward and SendRedirect.

Thanks in Advance.

Vijendra.
 
nandkishor rao
Ranch Hand
Posts: 53
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so new request means it must create new session?
will it creates new session for new request by using sendredirect
:roll:
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, session and requests are different things. Creating a new request doesn't mean creating a new session.
 
Sujith Kanaparthi
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic