What's the difference between response.sendRedirect and requestDispatcher.forward, requestDispatcher.include ?
First... What's the same?
Both of these methods cause a page, other than the one originally requested by the server to be displayed.
Differences
JavaDoc:javax.servlet.RequestDispatcher
With this method, the browser requests resource A.
Resource A does something and then forwards to resource B.
B does what it will do and then sends its response to the browser.
Points:
The browser has no knowledge that the forward took place.- The address window of the browser still containes the URL for resource A
The entire process takes place within one request/response cycle- The original request and response objects are available to resource B
- Fewer trips from the browser to server and back so faster (see notes on speed below)
Both resources must be part of the same context '(Some containers make provisions for cross-context communication but this tends not to be very portable)'
This method is at the heart of the MVC architecture for servlet/JSP applications and is what allows us to separate the various parts of our task and handle each with the most appropriate technology.
Because this takes place entirely on the server, firewalls and proxy servers can't interfere with the process.
JavaDoc:javax.servlet.http.HttpServletResponse
With this method, the browser makes a request for resource A.
Resource A does something and then, sends back a 301 or 302 response code (Moved permanently or moved temporarily) along with a 'Location' HTTP Response Header containing the URL for resource B.
The browser then issues a new request for resource B.
Points:
The browser, in this case, is doing the work and knows that it's making a new request- The browser's address window will display the URL for resource B
Because this involves a new request, it is possible for firewalls or proxy servers to dissallow this, causing unexpected behavior in your application (not common these days).
This method can be used to redirect users to resources that are not part of the current context, or even in the same domain.
Because this involves a new request, the original HttpServletRequest object, with all of its parameters and attributes will not be available to resource B. Variables will need to be passed by via the session object (if resource A and B are in the same context) or as querystring variables.
This method is a key part of the Post-Redirect-Get (or PRG) pattern. A technique used to insure that applications deal gracefully with browser refreshed and back button clicks.
Common Questions
Which is better?
This is like asking whether a pen or pencil is better. You need to understand both and decide which is the best tool for the task at hand.
Which is faster?
The forward method, because it involves fewer trips between the browser and server.
This should be the least important consideration when trying to decide which to use.
It is especially important that you don't switch from one to the other in an existing application to try to optimize it without understanding both the difference between the two and exactly what the application is doing with them.
Where can I find some examples or sample code that uses the two?
Because server side forwards are an important part of the Model, View, Controller architecture, you can find examples that use this in any of our
CodeBarn servlet applications that teach MVC.
SimpleMvc and
SimpleCommand are two of them.
There is a short example of the
PostRedirectGet pattern in our
JspFaq that uses the sendRedirect method.
ServletsFaq JspFaq