Friday 5 April 2013

When can we use forward and when can we use sendRedirect?



What is the difference between sendRedirect and forward in java?

forward

SendRedirect

Control can be transferred within the server

Control can be transferred to the other server also

The request and response objects are transferred with the forward

Request and response objects are not transferred with the sendRedirect

In case of forward URL is not refreshed

In case of sendRedirect the URL is refreshed

No new request is generated

New request is generated

Session is not lost in forward

Session is not lost in sendRedirect also



Where to use and where to use sendRedirect?
sendRedirect can be used in two scenarios
  1. You want to transfer control to resource on different server.
  2. Where you want to separate tasks as 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..
In all other cases Forward wil serve the purpose.

No comments:

Post a Comment