Difference between JSP forward and redirect

Ammu picture Ammu · May 20, 2011 · Viewed 75.1k times · Source

Please explain the difference between jsp:forward and redirect.
What is happening in each case?

Answer

Bozho picture Bozho · May 20, 2011
  • redirect sets the response status to 302 [1], and the new url in a Location header, and sends the response to the browser. Then the browser, according to the http specification, makes another request to the new url

  • forward happens entirely on the server. The servlet container just forwards the same request to the target url, without the browser knowing about that. Hence you can use the same request attributes and the same request parameters when handling the new url. And the browser won't know the url has changed (because it has happened entirely on the server)


[1]: This is an example of industry practice contradicting the standard. The HTTP/1.0 specification (RFC 1945) required the client to perform a temporary redirect (the original describing phrase was "Moved Temporarily"), but popular browsers implemented 302 with the functionality of a 303 See Other. Therefore, HTTP/1.1 added status codes 303 and 307 to distinguish between the two behaviours. However, some Web applications and frameworks use the 302 status code as if it were the 303. Source