A web services controller, implemented as an HttpServlet, may perform either a forward or a redirect operation at the end of processing a request. To understand the difference between these two cases, particularly how a browser reloads web pages:
- a forward is performed internally by the servlet
- the browser is completely unaware that it has taken place, so its original URL remains intact
- a browser reload of that page will only repeat the original request, using the original URL
- a redirect is a two step process, where the web application instructs the browser to fetch a second URL, which differs from the original
- when a browser reloads the second URL, it will not repeat the original URL request, but it will fetch the second URL
- redirect is slower than a forward, as it requires two browser requests, not one
- objects placed in the original request scope are lost and therefore not available to the second request
In general, use a forward command if the browser operation is repeatable and will return the same page; otherwise, use a redirect command. Therefore, if the user operation edits an item in the datastore, use a redirect, not a forward. This will avoid the possibility of duplicating a database transaction.
More explicitly (in terms of common SQL operations) :
- for SELECT operations, use a forward
- for INSERT, UPDATE, or DELETE operations, use a redirect
In HTML, a <FORM> tag can either GET or POST its data. Here, a GET command corresponds to a SELECT-then-forward, and a POSTcorresponds to an edit-then-redirect.
Forms for the input of search criteria should use GET, while forms to edit database records should use POST.