jump to navigation

Forward Vs Redirect August 12, 2006

Posted by :) in J2EE.
trackback

A Controller servlet may perform either a forward or a redirect operation at the end of processing a request.

Forward

  • forward is performed internally by the servlet
  • the browser is completely unaware that it has taken place, so its original URL remains intact
  • any browser reload will simple repeat the original request, with the original URL

Redirect

  • redirect is a two step process, where the web application instructs the browser to fetch a second URL, which differs from the original
  • a browser reload of the second URL will not repeat the original request, but will rather fetch the second URL
  • redirect is always slower than a forward, since it requires a second browser request
  • beans placed in the original request scope are not available to the second request

Forward should be used if the operation can be safely repeated upon reload; otherwise, redirect must be used. Typically, if the operation performs an edit on the datastore, then a redirect, not a forward, is required.

Comments»

1. Java Donkey - The Java Donkey comments on Java programming as well as other topics. » Struts Forward versus Redirect - April 17, 2007

[...] is an interesting blog regarding Forward vs Redirect This entry is filed under Struts. You can follow any responses to this entry through the RSS 2.0 [...]

2. ashish - April 25, 2007

Marvellous explanation. But just provide an example of both the methodology.

3. m - January 10, 2008

forward and include

Briefly explain what these two methods do and when you would use one or the other. You can use a picture to illustrate the different message paths.

Answer?

4. Java interview Questions « Java Stuff - September 13, 2008

[...] Forward Vs Redirect [...]

5. Michel - February 7, 2009

cool, thanks!