url-pattern in web.xml

http://blog.csdn.net/shangzhenhui/article/details/6119458

Servlet and filter are commonly used technologies in J2EE development, which are easy to use and simple to configure. There are some articles in the url-pattern in servlet and filter, summarizing some things, so as not to waste time when encountering problems. 
    First, the matching process of the servlet container to the url: 

When a request is sent to the servlet container, the container will first take the requested url minus the path of the current application context as the servlet mapping url, for example, I visit http://localhost /test/aaa.html, my application context is test, the container will remove http://localhost/test, and the remaining /aaa.html part will be used for servlet mapping matching. This mapping matching process is sequential, and when a servlet is successfully matched, the remaining servlets will not be ignored (the filter is different, which will be mentioned later). The matching rules and order are as follows: 

1. Exact path matching. Example: For example, the url-pattern of servletA is /test, and the url-pattern of servletB is /*. At this time, if the url I visit is http://localhost/test, the container will first match the exact path and find that /test is exactly matched by servletA, then call servletA and ignore other servlets. 

2. The longest path matches. Example: The url-pattern of servletA is /test/*, and the url-pattern of servletB is /test/a/*. At this time, when accessing http://localhost/test/a, the container will select the servlet with the longest path. Match, which is servletB here. 

3. Extension matching, if the last segment of the url contains an extension, the container will select the appropriate servlet according to the extension. Example: url-pattern of servletA: *.action 

4. If none of the previous three rules find a servlet, the container will select the corresponding request resource according to the url. If the application defines a default servlet, the container will drop the request to the default servlet (what is a default servlet? I'll talk about it later). 

     According to this rule table, the matching process of the servlet can be clearly known, so the url-pattern should also be considered when defining the servlet to avoid errors. 

      For filter, it will not match only one servlet like servlet, because the collection of filters is a chain, so only the order of processing is different, and only one filter will not be selected. Filters are processed in the same order as filter-mapping is defined in web.xml. 
    Second, the url-pattern is explained in detail 

         in the web.xml file. The following syntax is used to define the mapping: 

l. The ones starting with "/' and ending with "/*" are used for path mapping. 

2. The prefix "*." ” is used for extended mapping. 

3. “/” is used to define default servlet mapping. 

4. The rest are used to define detailed mapping. For example: /aa/bb/cc.action 

so , why is it wrong to define a seemingly normal match like "/*.action"? Because this match belongs to both path mapping and extension mapping, so the container cannot judge.

Guess you like

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