103. The difference between two kinds of URL acquisition, redirection (sendRedirect) and request forwarding (forward)

HttpServletResponse’s sendRedirect() and RequestDispater’s forward() allow the browser to obtain resources related to another URL, but they are different

The difference between request forwarding and redirection:

  1.  

  2. Request forwarding is one request and one response, while redirection is two requests and two responses.

  3.  

  4. The request forwarding address does not change, and the redirect will display the address of the next request

  5.  

  6. Request forwarding can only be forwarded to other Servlets in this project, and redirection can not only redirect to other Servlets in this project, but also to other projects

  7.  

  8. Request forwarding is a server-side behavior, only the Servlet path for forwarding needs to be given, and the redirection needs to give requestURI, which includes the project name!

  9.  

1.sendRedrect() redirection: not only can redirect to components in the same web application, but also redirect to other site resources. After redirection, the URL of the browser will change, because the essence of redirection is to send a message on the server side. Give the browser to re-browse the new URL

2.forward() request forwarding: forward can only request forwarding to components in the same web application. After forwarding, the url of the browser will not change. The essence of forward is to forward on the site to obtain other server resources and not to send to the browser. news

If the registration fails, jump back to the registration interface

req.getRequestDispatcher(path).forward(req, resp); path取值:"/pages/user/regist.html"

Guess you like

Origin blog.csdn.net/weixin_43206161/article/details/111815726