• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Difference between forward and SendRedirect?

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can Anyone explain Difference between forward and SendRedirect.

Thanks in Advance.

Vijendra.
 
Ranch Hand
Posts: 53
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic