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

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

Ninja Asked on 19th September 2018 in Servlet.
Add Comment
1 Answer(s)
Best answer

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.

The forward() is handled internally by the container whereas sendRedirect() is handled by browser. We should use forward() when accessing resources in the same application because it’s faster than sendRedirect() method that required an extra network call.

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. The forward() can’t be used to invoke a servlet in another context, we can only use sendRedirect() in this case.

Ninja Answered on 19th September 2018.
Add Comment