Forward vs Redirect

Share

A Controller servlet may perform either a forward or a redirect operation at the end of processing a request in order to get to the results page.

Let’s say a browser requests page “A” in an application. If the application responds by forwarding to “B”, then a
server-side forward takes place; the server sends back the contents of “B” but the browser doesn’t know the difference. It asked for “A” and got something back that happened to actually
be “B”.
When redirect is invoked, it causes the web container to return to the browser a response indicating that a new URL should be requested. Because the browser issues a completely new request, any objects that are stored as request attributes before the redirect occurs will be lost. This is one of the biggest differences between a forward and a redirect.
If the applications responds by redirecting to “B”, then the server sends back a message saying “please ask me for B”. The browser then requests “B” and gets it.
Either way it gets the same thing, but the mechanism is quite different.

An example of when you need redirect that comes to mind is when
you want to forward to a URL on another server…(suppose you need some data from googlemaps) you can’t do a server-side forward in that case.
Or,another case, if the operation performs an edit on the datastore, then a redirect, not a forward, is required. This is simply to avoid the possibility of accidentally duplicating an edit to the database.

Advantages of using redirect over a forward

  • when you “refresh” the page it only refreshes the page that was redirected to. Often after a “save” request, you redirect to a “display” page. If that page is refreshed it just redoes the “display”, rather than “save and display” which is what would happen if you used a forward.
  • no confusion over relative links.

The advantages of using a forward over a redirect

  • doesn’t require a round trip for the client to reissue a new request. A redirect obviously involves some more overhead to get from A to B, so to speak.
  • retains all request attributes/parameters (lost on redirect)

Disadvantage:

  • can be confusing for relative urls from your pages. ie you request “/saveUser” which forwards to “/jsp/user/saveSuccess.jsp”. Relative links would be resolved from “/saveUser” which can be confusing. Solution – use an html <base> tag
  • the “refresh” problem

Below is a table with differences between forward and redirect, as seen on here

Forward Redirect
* forward is performed internally by the servlet * redirect is a two step process, where the web application instructs the browser to fetch a second URL, which differs from the original
* the browser is completely unaware that it has taken place, so its original URL remains intact * a browser reload of the second URL will not repeat the original request, but will rather fetch the second URL
* any browser reload will simple repeat the original request, with the original 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

Finally, there’s another very important peculiarity of what does Cialis that brings it so high above its alternatives. It is the only med that is available in two versions – one intended for use on as-needed basis and one intended for daily use. As you might know, Viagra and Levitra only come in the latter of these two forms and should be consumed shortly before expected sexual activity to ensure best effect. Daily Cialis, in its turn, contains low doses of Tadalafil, which allows to build its concentration up in your system gradually over time and maintain it on acceptable levels, which, consequently, makes it possible for you to enjoy sex at any moment without having to time it.

By continuing to use the site, you agree to the use of cookies. More information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close