Difference between forward and sendRedirect
SR
|
sendRedirect
|
forward
|
1
|
sendRedirect() sends a redirect response back to the client's browser. The browser will normally interpret this response by initiating a new request to the redirect URL given in the response
|
forward() does not involve the client's browser. It just takes browser's current request, and hands it off to another servlet/jsp to handle. The client doesn't know that they're request is being handled by a different servlet/jsp than they originally called.
|
2
|
If you want the browser to initiate a new request to a different servlet/jsp, or if the servlet/jsp you want to forward to is not in the same web application, use sendRedirect().
|
if you want to hide the fact that you're handling the browser request with multiple servlets/jsp, and all of the servlets/jsp are in the same web application, use forward() or include().
|
3
|
This is happend at Client Side .
|
This happened in Server side
|
4
|
In this case existing request and response are lost
|
In this case existing request and response are preserve
|
5
|
in redirect, you can see the redirected address.
|
In forward, you will not see the forwarded address (since the browser is not involved)
|
When can we use forward and when can we use sendRedirect?
Technical scenario: redirect should be used
- If you need to transfer control to different domain
- To achieve separation of task.
For example, database update and data display can be separated by redirect. Do the PaymentProcess and then redirect to displayPaymentInfo. If the client refreshes the browser only the displayPaymentInfo will be done again and PyamenProcess will not be repeated. But if you use forward in this scenario, both PaymentProcess and displayPaymentInfo will be re-executed sequentially, which may result in incosistent data.
No comments:
Post a Comment