Differences of Redirect and Forward
Redirection and Forward is one of the most fundamental technique required in developing java web application. Be it in examination or during interviews for a web developer position or in real life web application, you will never run away from this question.
Forward:
- It is an action that performed internally by the servlet between the same web container.
- Client web browser is never aware of the changes taken place therefore browser URL never changes.
- Reload of the resulting page will simple repeat the original request, with the original URL
Redirect:
- Upon receive of the first URL request, servlet instruct client browser to fetch a new URL, which is explicitly done.
- The browser reload will not repeat the original request, which is the first URL; but the new URL will be reloaded.
- Objects placed in the original request scope are not available to the second request.
- It is slower as two actions are required.
When and where should I use forward or redirect, I heard you ask. Well, if you need fast response and safety is not an issue during reload of page, Mr.Forward is the lightning man. On the otherhand where safety and security of data is of vital important such as any operation on data store, Mr. Redirect the security guard is required to prevent duplicating edit to database.
This is also why, in coding, Response object is use for a redirection while forward is handle by RequestDispatcher; Obviously, we will need to encodeURL before redirection but not forward; and states are kept in forward but not redirection.