jQuery in Action, 2nd edition*
The moose likes Servlets and the fly likes sendRedirect vs Foward Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "sendRedirect vs Foward" Watch "sendRedirect vs Foward" New topic
Author

sendRedirect vs Foward

Leonardo Pilatti
Greenhorn

Joined: Jul 07, 2002
Posts: 6
Does anyone knows the diference between RequestDispatcher.foward and HttpServletResponse.sendRedirect?
I mean in any terms (performance, funcionability, etc).
thanks in advice
Leonardo Pilatti
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

In a nutshell; a forward routes the request instance directly to the specified resource, while a redirect causes the browser to make a new request to the specified resource.
There are other nuances, but the main difference is that the foward takes place in the context of a single request (meaning that any request attributes you have placed on the request remain intact), versus the redirect which spawns a new request.
hth,
bear
[ December 03, 2002: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Isaias C. Barroso
Ranch Hand

Joined: Jan 22, 2002
Posts: 165
Hi,
With RequestDispatche().foward(), you can call only resources of your server.
With HttpServletResponse.sendRedirect, you can call resources out of you server. Example:
response.sendRedirect("http://site.com")
By


Isaias
Leonardo Pilatti
Greenhorn

Joined: Jul 07, 2002
Posts: 6
Thanks Isaias and Bibeault
Mark Stein
Ranch Hand

Joined: May 20, 2002
Posts: 75
Two other things to keep in mind...
forward takes the argument (ServletRequest, ServletResponse) so parameters, session, etc. can be passed.
sendRedirect doesn't pass anything with it (unless you write it onto the end of the URL).
Also, forward is transparent to the browser, while sendRedirect isn't.
MS
 
Consider Paul's rocket mass heater.
 
subject: sendRedirect vs Foward