Servlet URL mapping rules

First, the matching process of the Servlet container to the URL:

  When the client sends a request to the servlet container, the container will use the requested URL minus the path of the current web application as the servlet mapping, find the corresponding servlet and process the response, such as the accessed url is http://localhost: 8080/FirstWebDemo/index.jsp, then the container gets the servlet mapping as /index.jsp. After the matching rule is successful, other servlets are ignored, among them.

2. Path matching categories and matching rules:

  1. Exact Path Matching

  /first                     http://localhost:8080/ProjectName/first

  /xxx/demoServlet          http://localhost:8080/ProjectName/xxx/demoServlet

  2. Fuzzy Path Matching

  /* http://localhost:8080/ProjectName/any path

  /test/* http://localhost:8080/ProjectName/test/any path

  *. Suffix http://localhost:8080/ProjectName/any path.do such as: *.do *.action *.html (pseudo-static)

  3. Matching rules

  (1) The mapping of Servlet either starts with / or starts with *. Only writing news such as news is an illegal path, and writing two fuzzy matches at the same time is an illegal path, such as /news/*.action.

  (2) When the input URL is matched by multiple servlets at the same time:

    The highest priority is exact path matching, followed by fuzzy path matching. For example, the mapping of Servlet1 is /index.jsp, and the mapping of Servlet2 is /*, then the client sends a request http://localhost/index.jsp and / index.jsp is matched, and other servlets are ignored after the match is successful.

    The longest path is matched first. Under the same matching conditions, both are exact path matching or fuzzy matching. For example, the mapping of Servlet1 is /news/*, and the mapping of Servlet2 is /news/normalnews/*, then the client sends a request http://localhost/news /normalnews will match Servlet2.

    The mapping at the end of the suffix name in fuzzy matching has the lowest priority.

Note: / is a predefined mapping path in the servlet: the default mapping path of the servlet (<url-pattern>/</url-pattern>) is a mapping path built into the tomcat server. This path corresponds to a DefaultServlet (default Servlet). The role of this default servlet is to parse the static resource files of the web application. In short, look for dynamic resources first, and then look for static resources when dynamic resources do not exist.

 

Reference from: https://www.cnblogs.com/fnz0/p/5586019.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325389369&siteId=291194637