Servlet-JSP

Difference between SendRedirect and Forward

25 Oct , 2016  

Now let’s see some difference between these two method of servlet API in tabular format.

 

Forward() SendRediret()
When we use forward method request is transfer to other resource within the same server for further processing. In case of sendRedirect request is transfer to another resource to different domain or different server for futher processing.
In case of forward Web container handle all process internally and client or browser is not involved. When you use SendRedirect container transfers the request to client or browser so url given inside the sendRedirect method is visible as a new request to the client.
When forward is called on requestdispatherobject we pass request and response object so our old request object is present on new resource which is going to process our request In case of SendRedirect call old request and response object is lost because it’s treated as new request by the browser.
Visually we are not able to see the forwarded address, its is transparent In address bar we are able to see the new redirected address it’s not transparent.
Using forward () method is faster then send redirect. SendRedirect is slower because one extra round trip is required beasue completely new request is created and old request object is lost.Two browser request requird.
When we redirect using forward and we want to use same data in new resource we can use request.setAttribute () as we have request object available. But in sendRedirect if we want to use we have to store the data in session or pass along with the URL.

 

Which one is good?

Its depends upon the scenario that which method is more useful.

If you want control is transfer to new server or context and it is treated as completely new task then we go for Send Redirect.

Normally forward should be used if the operation can be safely repeated upon a browser reload of the web page will not affect the result.

SendRedirect and forward method are still very useful while programming or working on any web application project using servlet jsp. This is still a popular interview questions so don’t forget to revise forward and sendRedirect before appearing for any job interview.


Leave a Reply

Your email address will not be published. Required fields are marked *