原文地址:https://www.programmersought.com/article/8728115849/
Forwarding is the same request (requesting the next resource by the server)
Redirect is a multiple request (redirected by the server to the client, the client requests the next resource)
A summary of the sentence: forwarding is a request from the server, the redirect is made by the client
Note: Forwarding and redirecting URLStrings are preceded by plus / for absolute paths and vice versa for relative paths
对于Redirect
-
Assume that the specified Url resource is requested via the form action="LoginServlet"
The request address generated by the form is:Http://localhost:8080/project name/LoginServlet
-
Redirect to the specified resource below after the servlet processes the request. Parameters can be specified as relative paths or absolute paths or web applications.
-
relative path: response.sendRedirect("Manager/index.jsp") Generated address: The original request address + parameter generates the complete URL: Http://localhost:8080/project name/Manager/index.jsp
-
absolute path: response.sendRedirect("/Manager/index.jsp") The generated address: the web server's own address + parameters to generate the complete URL ie: http://localhost:8080/Manager/index.jsp
-
-
other web application addresses
The container is directed to the URL: response.sendRedirect(“http://www.baidu.com”)
对于Forward
Forward to the specified resource below after the servlet processes the request. The parameter can be specified as a relative path or an absolute path.
- relative path
- The full URL generated in the case of a relative path is the same as the redirect method.
- absolute path
- Absolute path is different from redirection, the container will generate the full URL relative to the root directory of the web application. Generated address: The original request address + parameter generates the complete URL: Http://localhost:8080/project name/Manager/index.jsp Forwarding is the same for both relative and absolute path addresses.
- ServletContext.getRequestDispatcher(UrlString)The parameter can only be specified as an absolute path, and the resulting full URL is the same as HttpServletRequest.getRequestDispatcher(String).