There are following difference between sendRedirect() and RequestDispatcher forward() method.
RequestDispatcher forward() :-
- The RequestDispatcher forward() method is work at a server-side.
- The RequestDispatcherforward() method is used to show a different resource in place of the servlet that was originally called.
- RequestDispatcher forward() is used to forward the same request to another resource.
- RequestDispatcher forward() is handled internally by the servlet container.
- We will use forward() when accessing the resources in the same application because it’s faster than sendRedirect().
- In forward() browser is unaware of the actual processing resource and the URL in address bar remains same.
- forward() can’t be used to invoke a servlet in another servlet context.
sendRedirect() :-
- sendRedirect() method is works at client side.
- In sendRedirect(), web application returns the response to client with status code 301 (redirect) with URL to send the request. the request sent is a completely new request.
- sendRedirect is slower than RequestDispatcher because it is required extra request network call.
- In sendRedirect() method URL in address bar change at the forwarded resource.
- sednRedirect() is handled by web browser.
- sendRedirect() can be used to invoke a servlet in another context.
- For example, sendRedirect can transfer control from http://javatechnote.com to http://otherdomain.com but forward cannot do this type.