When we are working with a java web application using jsp, we submit the request and get the response. But there are 2 ways in which request can be submitted.
1)Forward
2)Redirect
So what is the difference between the two?
The Forward method forwards a request from one servlet to another resource in a web application and this resource can be another servlet, JSP page, or HTML file. So in forward, the server or you can say the web container handles the forwarding of the request from one resource to another.
The redirect method on the other hand redirects the request to a different URL provided by us. The important point here is that the redirection is done by the browser and not the server. The URL can be part of the same web application hosted in the server or it can be complete different application.
The forward method is declared in the RequestDispatcher.
The SendRedirect( ) method is declared in HttPServletResponse and is used to redirect the client request to a different URL which can be available on a different server or same server. With a redirect, you can redirect the browser to a different application altogether.
When forward is called on requestdispather object 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.
Session is not lost in both forward and redirect!!!!