forward runs on the server side while sendRedirect runs on the client as well as on the server side thats why the response generated by sendRedirect() is slow as compared to rd.forward().By using sendRedirect() you can forward the request to any web application either in the same server or to the another one.Incase of forward() the request has to be forwarded to the same web application.One more difference is that forward() method holds the previous request and response objects while using sendRedirect(),it will create fresh request and response objects.
response.sendRedirect() sends a response to the browser asking it to load another page, whereas in the case of RequestDipsatcher.forward() control is transferred to another servlet or jsp within the server. Using sendRedirect() on one server, we call redirect a call to a resource on located on different server which is not possible using forward(). Hope this may help you.
Joined: Apr 17, 2006
A simple example which shows the difference between them will make my concepts about them more clear. Can anyone list down a example plz.
Imagine soccer training (world cup is coming). PlayerA has a ball. To pass the ball from playerA to playerB, there are two possibilites: 1. PlayerA passes his ball to playerB (forward) 2. PlayerA grabs a brand new ball and sends it to playerB (sendRedirect)
With forward, you are passing the existing request, with everything it already contains (headers, attributes...) With sendRedirect, you are creating a new request (new headers, no attributes).
No, session and requests are different things. Creating a new request doesn't mean creating a new session.
Joined: Sep 04, 2005
As clarified by Satou kurinosuke, session and request are different things.
When forward is used we can pass data to the forwarded page using request.setAttribute() as the same request and response objects available the forwarded servlet/jsp.But in in case of sendRedirect() we have to set the data in session or by appending the data to the URL that will be passed as argument to this method, because by calling this method a new request is created.