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>
What is difference between HTTP Redirect and Forward
Upasana | August 05, 2019 | 2 min read | 28 views
Http Forward
-
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.
-
When forward is done, the original request and response objects are transferred along with the additional parameters if needed.
-
Forward can’t transfer control to some other domain.
-
Original URL at the client side remains intact, refreshing the page will cause the whole step to repeat again.
-
Session object is not lost in forward or redirect.
Http Redirect
-
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.
-
Server sends Http Status Code of 301 to the client, and then client follows the instructions.
-
If someone reloads the page on browser, then original request will not be repeated. Just the second url will be fetched again.
-
Redirect is marginally slower than forward, since it requires two requests.
-
Objects placed in request scope are not available to second request.
-
There are several ways to perform a redirect for example
Redirect Should be used when
-
If you need to transfer of control to a different domain.
-
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:
- What is difference between Primary key and Unique Key
- Java Concurrency Interview Questions
- Difference between method overloading and overriding in Java
- Multi-threading Java Interview Questions for Investment Bank
- Difference between getOne and findById in Spring Data JPA?
- Cracking core java interviews - question bank
- Sapient Global Market Java Interview Questions and Coding Exercise