FORWARD vs REDIRECT logic in Web Services

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:

Forward

  • 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

Redirect

  • 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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s