RequestDispatcher.forward() vs HttpServletResponse.sendRedirect()

前端 未结 9 821
萌比男神i
萌比男神i 2020-11-22 03:18

What is the conceptual difference between forward() and sendRedirect()?

9条回答
  •  花落未央
    2020-11-22 03:20

    Simply difference between Forward(ServletRequest request, ServletResponse response) and sendRedirect(String url) is

    forward():

    1. The forward() method is executed in the server side.
    2. The request is transfer to other resource within same server.
    3. It does not depend on the client’s request protocol since the forward () method is provided by the servlet container.
    4. The request is shared by the target resource.
    5. Only one call is consumed in this method.
    6. It can be used within server.
    7. We cannot see forwarded message, it is transparent.
    8. The forward() method is faster than sendRedirect() method.
    9. It is declared in RequestDispatcher interface.

    sendRedirect():

    1. The sendRedirect() method is executed in the client side.
    2. The request is transfer to other resource to different server.
    3. The sendRedirect() method is provided under HTTP so it can be used only with HTTP clients.
    4. New request is created for the destination resource.
    5. Two request and response calls are consumed.
    6. It can be used within and outside the server.
    7. We can see redirected address, it is not transparent.
    8. The sendRedirect() method is slower because when new request is created old request object is lost.
    9. It is declared in HttpServletResponse.

提交回复
热议问题