|
|
||||
|
||||
|
|
||||
|
||||
|
|
|
|
||||
|
||||
|
|
||||
|
||||
|
|
Redirect Forward | |
|
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 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:
HttpServletResponse.sendRedirect 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:
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.
| |