File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "sendRedirect vs Foward" Watch "sendRedirect vs Foward" New topic

sendRedirect vs Foward

Leonardo Pilatti

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

Joined: Jan 10, 2002
Posts: 63873

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.
[ December 03, 2002: Message edited by: Bear Bibeault ]

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

Joined: Jan 22, 2002
Posts: 165
With RequestDispatche().foward(), you can call only resources of your server.
With HttpServletResponse.sendRedirect, you can call resources out of you server. Example:

Leonardo Pilatti

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.
I agree. Here's the link:
subject: sendRedirect vs Foward
It's not a secret anymore!