RequestDispatcher.forward() vs HttpServletResponse.sendRedirect()

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

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

相关标签:
9条回答
  • 2020-11-22 03:43

    The RequestDispatcher interface allows you to do a server side forward/include whereas sendRedirect() does a client side redirect. In a client side redirect, the server will send back an HTTP status code of 302 (temporary redirect) which causes the web browser to issue a brand new HTTP GET request for the content at the redirected location. In contrast, when using the RequestDispatcher interface, the include/forward to the new resource is handled entirely on the server side.

    0 讨论(0)
  • 2020-11-22 03:45

    The main important difference between the forward() and sendRedirect() method is that in case of forward(), redirect happens at server end and not visible to client, but in case of sendRedirect(), redirection happens at client end and it's visible to client.

    0 讨论(0)
  • 2020-11-22 03:46

    Technically redirect should be used either if we need to transfer control to different domain or to achieve separation of task.

    For example in the payment application we do the PaymentProcess first and then redirect to displayPaymentInfo. If the client refreshes the browser only the displayPaymentInfo will be done again and PaymentProcess will not be repeated. But if we use forward in this scenario, both PaymentProcess and displayPaymentInfo will be re-executed sequentially, which may result in incosistent data.

    For other scenarios, forward is efficient to use since as it is faster than sendRedirect

    0 讨论(0)
提交回复
热议问题