Simply difference between Forward(ServletRequest request, ServletResponse response)
and sendRedirect(String url)
is
forward():
- The
forward()
method is executed in the server side.
- The request is transfer to other resource within same server.
- It does not depend on the client’s request protocol since the
forward ()
method is provided by the servlet container.
- The request is shared by the target resource.
- Only one call is consumed in this method.
- It can be used within server.
- We cannot see forwarded message, it is transparent.
- The
forward()
method is faster than sendRedirect()
method.
- It is declared in
RequestDispatcher
interface.
sendRedirect():
- The sendRedirect() method is executed in the client side.
- The request is transfer to other resource to different server.
- The sendRedirect() method is provided under HTTP so it can be used only with HTTP clients.
- New request is created for the destination resource.
- Two request and response calls are consumed.
- It can be used within and outside the server.
- We can see redirected address, it is not transparent.
- The sendRedirect() method is slower because when new request is created old request object is lost.
- It is declared in HttpServletResponse.