RequestDispatcher.forward() vs HttpServletResponse.sendRedirect()

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

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

9条回答
  •  灰色年华
    2020-11-22 03:41

    Either of these methods may be "better", i.e. more suitable, depending on what you want to do.

    A server-side redirect is faster insofar as you get the data from a different page without making a round trip to the browser. But the URL seen in the browser is still the original address, so you're creating a little inconsistency there.

    A client-side redirect is more versatile insofar as it can send you to a completely different server, or change the protocol (e.g. from HTTP to HTTPS), or both. And the browser is aware of the new URL. But it takes an extra back-and-forth between server and client.

提交回复
热议问题