It programmer development must-all kinds of resources download list, the most complete IT resources in history, personal collection summary.
The use rules of the wildcard character * in Url-pattern:
(1) The same servlet can be mapped to multiple URLs, that is, the set value of <servlet-name> child elements of multiple <servlet-mapping> elements can be the registered name of the same servlet.
(2) You can also use the * wildcard character in a URL that the servlet maps to, but there can be only two fixed formats: one format is "*. Extension" and the other is preceded by a forward slash (/) and ends with "/*".
<servlet-mapping>
<servlet-name>AnyName</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AnyName</servlet-name>
<url-pattern>/action/*</url-pattern>
</servlet-mapping>
The process of matching a servlet container to a URL:
When a request is sent to the servlet container, the container first subtracts the requested URL from the current application context as the servlet's mapped URL, for example, I am visiting the http://localhost/test/ aaa.html, my application context is test, the container will remove the http://localhost/test, and the rest of the/aaa.html part to do the mapping of the servlet. This mapping matching process is sequential, and when a servlet match succeeds, the rest of the servlet is not heeded (filter is different, which is mentioned later). The matching rules and Order are as follows:
1. Exact path matching. Example: For example Servleta's url-pattern for/TEST,SERVLETB url-pattern for/*, this time, if I access the URL for http://localhost/test, This time the container will be the exact path matching, found that/test exactly by Servleta Exact match, then go to call Servleta, will not ignore the other servlet.
2. Maximum path matching . Example: Servleta's Url-pattern is/test/*, and Servletb Url-pattern is/test/a/*, when you visit http://localhost/test/a, The container chooses the longest servlet in the path to match, which is the SERVLETB here.
3. Extension matches , if the last paragraph of the URL contains an extension, the container will select the appropriate servlet based on the extension. Example: Servleta's Url-pattern:*.action
4. If none of the preceding three rules finds a servlet, the container chooses the corresponding request resource based on the URL. If the application defines a default servlet, the container throws the request to the default servlet (what is the default servlet). See the default mapping Path "/" issue in the Web.xml file and the matching rules for client access to Web resources.
According to this rule table, you can know the matching process of the servlet clearly, so when you define the servlet, you should also consider the Url-pattern to avoid error.
For filter, it does not match a servlet as a servlet, because the filter's collection is a chain, so there is only a different order of processing, and no only one filter is selected. The processing order of the filter is the same as the order defined in the filter-mapping in Web.xml.
Second, Url-pattern detailed
In the Web.xml file, the following syntax is used to define mappings:
L. With the "/ " and the "/*" End is used to do path mapping .
2. With the prefix "*." The beginning is used to do the extension mapping .
3. "/" is used to define the default servlet mapping .
4. The remainder is used to define detailed mappings . For example:/aa/bb/cc.action
So, why is the definition of "/*.action" such that a seemingly normal match is an error when starting Tomcat. Because this match belongs to the path map, it is also an extension map, causing the container to be unable to judge .
three. Example (lowest priority for *.do)
for some mapping relationships as follows:
Servlet1 map to/abc/*
Servlet2 Map to/*
Servlet3 map to/ABC
Servlet4 map to *.do
issues:
when the request URL is "/abc/a.html", "/abc/*" and "/*" are matched, which Servlet responds? The
servlet engine will invoke Servlet1.
when the request URL is "/ABC", both "/abc/*" and "/ABC" match, which Servlet responds? The
servlet engine will invoke Servlet3.
when the request URL is "/abc/a.do", both "/abc/*" and "*.do" match, which Servlet responds? The
servlet engine will invoke Servlet1.
when the request URL is "/a.do", "/*" and "*.do" match, which Servlet responds? The
servlet engine will invoke Servlet2.
when the request URL is "/xxx/yyy/a.do", "/*" and "*.do" match, which Servlet responds? The
servlet engine will invoke Servlet2.
Also, after the Url-pattern mapping, request Servletcontextpath, Servletpath, PathInfo case, can refer to the article linked below http:// blog.csdn.net/xh16319/article/details/8014193