Saturday, 5 September 2015

What is difference between ServletResponse sendRedirect() and RequestDispatcher forward() method?

  1. RequestDispatcher forward() is used to forward the same request to another resource whereas ServletResponse sendRedirect() is a two step process. In sendRedirect(), web application returns the response to client with status code 302 (redirect) with URL to send the request. The request sent is a completely new request.
  2. forward() is handled internally by the container whereas sednRedirect() is handled by browser.
  3. We should use forward() when accessing resources in the same application because it’s faster than sendRedirect() method that required an extra network call.
  4. In forward() browser is unaware of the actual processing resource and the URL in address bar remains same whereas in sendRedirect() URL in address bar change to the forwarded resource.
  5. forward() can’t be used to invoke a servlet in another context, we can only use sendRedirect() in this case.

No comments:

Post a Comment