What is difference between HTTP Redirect and Forward
Carvia Tech | August 05, 2019 | 2 min read | 28 views
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.
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
Find more on this topic:
Interview - Product Companies, eCommerce Companies, Investment Banking, Healthcare Industry, Service Companies and Startups.
Last updated 1 week ago
Subscribe to Interview Questions
Recommended books for interview preparation:
- Finastra Investment Banking Interview Questions
- Merge two sorted array into a single sorted array
- Spring Boot with GMAIL SMTP
- Mandrill emails in Spring Boot Java
- Hibernate & Spring Data JPA interview questions
- Generating cryptographically strong key/secret in Java
- Reverse the bits of a number and check if the number is palindrome or not
- MD5 and SHA256 in Java Kotlin and Android
- There is no PasswordEncoder mapped for the id
- Inter-thread communication in Java