Servlet forwarding and redirection paths

Source: Internet
Author: User

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.

  1. Java Bean and Servlet in JSP
  2. Draft final suggestions for Servlet 3.0 specifications released
  3. GET requests in the basic structure of Servlet
  4. Analysis of multi-language display in Servlet and Jsp
  5. High-performance, high-flexibility JSP and Servlet Performance Optimization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.