What is difference between HTTP Redirect and Forward

Upasana | August 05, 2019 | 2 min read | 28 views


Http Forward

  1. Control is forwarded to the resource available within the server from where the call is made, the transfer of control is made internally by the container, where client is completely unaware that a forward is happening.

  2. When forward is done, the original request and response objects are transferred along with the additional parameters if needed.

  3. Forward can’t transfer control to some other domain.

  4. Original URL at the client side remains intact, refreshing the page will cause the whole step to repeat again.

  5. Session object is not lost in forward or redirect.

Http Redirect

  1. A redirect is a two step process where web application instructs the browser client to fetch the fetch the second URL which differs from the original.

  2. Server sends Http Status Code of 301 to the client, and then client follows the instructions.

  3. If someone reloads the page on browser, then original request will not be repeated. Just the second url will be fetched again.

  4. Redirect is marginally slower than forward, since it requires two requests.

  5. Objects placed in request scope are not available to second request.

  6. There are several ways to perform a redirect for example

    Http/1.1 301 moved permanently
    Location : http://www.foobar.org/
    <meta http-equiv="refresh" Content="0; URL=http://www.example.org/"/>
    <script type="text/javascript"> location.href ="http://www.foobar.org" </script>

Redirect Should be used when

  1. If you need to transfer of control to a different domain.

  2. To achieve separation of tasks.

For example, database update and data display can be separated by redirect. In this case if user presses F5 button the browser then only display part will execute again and not the database update part. Do the PaymentProcess and then redirect to the display payment info, if the client refreshes the browser, only the displayInfo will be done again and not the payment process. Use Forward when database SELECT operations are used


Top articles in this category:
  1. What is difference between Primary key and Unique Key
  2. Java Concurrency Interview Questions
  3. Difference between method overloading and overriding in Java
  4. Multi-threading Java Interview Questions for Investment Bank
  5. Difference between getOne and findById in Spring Data JPA?
  6. Cracking core java interviews - question bank
  7. Sapient Global Market Java Interview Questions and Coding Exercise

Recommended books for interview preparation:

Find more on this topic:
Buy interview books

Java & Microservices interview refresher for experienced developers.