• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

forward Vs redirect

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whats the difference between requestDispatcher.forward() and response.redirect
in which situations are either of these used ?
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
forward takes the current request and response objects and sends them to another resource which then handles the request as it sees fit.
sendRedirect sends a response back to the client that causes the clients browser to send a new request the the provided URL.

------------------
I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In forward, we can only specify the resources that are in the context of the web application, but using sendRedirect we can forward the request to any other resource which is not in the current context. Is this correct?
Thanks.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
response.sendRedirect() sets the http headers with the location="movedurl" and status accordingly and it lefts to the browser to fetch the moved page. after this method call response is closed any attempt to write will throws an Exception.
Where as requestDispatcher.forward() forwards internally (ie, browser wont come into picture) with this you can forward to only those servlets which are in the same ServletContext as Forwarding Servlet. 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.
Sridhar

Originally posted by Luna Bora:
In forward, we can only specify the resources that are in the context of the web application, but using sendRedirect we can forward the request to any other resource which is not in the current context. Is this correct?
Thanks.


 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RequestDispatcher.forward() forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server. This method allows one servlet to do preliminary processing of a request and another resource to generate the response.
whereas response.sendRedirect()sends a temporary redirect response to the client using the specified redirect location URL. This method can accept relative URLs, hence the servlet container must convert the relative URL to an absolute URL before sending the response to the client.

Manish Singhal
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just have two things to add. If you use reDirect() (since you are involving the browser) the address in the browser changes, and hence the user can refresh that page properly.
If you forward, it may be quicker since you save a round-trip, but if the user hits refresh, he will move backwards to the page still in the address bar.
Also, objects in request scope are lost on a redirect, but remain in scope after a forward.
[This message has been edited by mitchner green (edited October 24, 2001).]
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay! i have one more doubt, when to opt for forward and when should we opt for sendRedirect()
Thnks
 
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Tausif Khanooni:
okay! i have one more doubt, when to opt for forward and when should we opt for sendRedirect()
Thnks


If the destination is a resource in the context of the web app, it results better performance if u use forward... But for the resource out the web app, we have to use sendRedirect with no more choice...
Hope this helps...
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
sendRedirect() keeps the context of the ressources , but forward not.
If you understand french, you may have a look to:
this article
Regards,
Cyril.
 
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Manish Singhal:
whereas response.sendRedirect()sends a temporary redirect response to the client using the specified redirect location URL. This method can accept relative URLs, hence the servlet container must convert the relative URL to an absolute URL before sending the response to the client.
Manish Singhal


What is exactly meant by a relative URL here? Does this mean that the resource the user is being forwarded to exists on the same server?
Please clarify.
 
I'm THIS CLOSE to ruling the world! Right after reading this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic