There are two ways to obtain the Servlet forwarding object (RequestDispatcher): one is obtained through the getRequestDispatcher () method of HttpServletRequest, and the other is obtained through the getRequestDispatcher () method of ServletContext;
Servlet redirection has only one method: HttpServletResponse's sendRedirect () method.
The parameters of these three methods are URL strings, but they are different in relative or absolute paths.
◆ HttpServletResponse. sendRedirect (String)
Parameters can be specified as relative paths, absolute paths, or other Web applications.
Assume that the Servlet to which the method belongs is obtained through the http: // localhost/myApp/cool/bar. do request.
Relative Path: response. sendRedirect ("foo/stuff. do ")
The container adds parameters to the directory of the original request URL to generate the complete URL-http: // localhost/myApp/cool/foo/stuff. do.
Absolute path: response. sendRedirect ("/foo/stuff. do ")
Create a complete URL -- http: // localhost/foo/stuff. do.
Other Web applications: response. sendRedirect ("http://www.xxx.com ")
The container directs the URL directly.
◆ HttpServletRequest. getRequestDispatcher (String)
The parameter can be specified as a relative or absolute path.
The complete URL generated in relative paths is the same as the redirection method.
The absolute path is different from Servlet redirection. The container will add parameters to the root directory of the Web application to generate a complete URL, namely:
The URL generated by request. getRequestDispatcher ("/foo/stuff. do") is http: // localhost/myApp/foo/stuff. do.
◆ ServletContext. getRequestDispatcher (String)
The parameter can only be specified as an absolute path. The generated complete URL is the same as HttpServletRequest. getRequestDispatcher (String.
- Java Bean and Servlet in JSP
- Draft final suggestions for Servlet 3.0 specifications released
- GET requests in the basic structure of Servlet
- Analysis of multi-language display in Servlet and Jsp
- High-performance, high-flexibility JSP and Servlet Performance Optimization