Url-pattern matching rules in web. xml, web. xmlurl-pattern

Source: Internet
Author: User

Url-pattern matching rules in web. xml, web. xmlurl-pattern

Today, I just want to write a filter to verify whether the user logs in. So I got familiar with the url-pattern matching rules in web. xml:

1. url matching process of the servlet container:
When a request is sent to the servlet container, the container first uses the requested url minus the current application context path as the servlet ing url. For example, I access http: // localhost/test/aaa.html. My application context is test. The Container removes http: // localhost/test, and the rest of the/aaa.html part is used for servlet ing matching. This ing matching process is sequential, and when a servlet matches successfully, it will not ignore the remaining servlet (different filters will be mentioned later ). The matching rules and sequence are as follows:

1. Exact path matching.

Example: for example, if the url-pattern of servletA is/test and the url-pattern of servletB is/*, if the url I access is http: // localhost/test, in this case, the container will first perform exact Path Matching and find that/test is exactly matched by servletA. Then, the container will call servletA and ignore other servlets.


2. Longest Path Matching.

Example: The url-pattern of servletA is/test/*, while the url-pattern of servletB is/test/a/*. access http at this time: // localhost/test/a, the container selects the servlet with the longest path to match, that is, the servletB here.


3. Extended matching

If the last segment of the url contains extensions, the container selects the appropriate servlet Based on the extension. Example: servletA url-pattern: *. action


4. If no servlet is found in the first three rules, the container selects the corresponding request Resource Based on the url. If the application defines a default servlet, the container will throw the request to the default servlet (what is the default servlet? Later ).

According to this rule table, you can clearly understand the servlet matching process. Therefore, when defining the servlet, you must also consider the url-pattern method to avoid errors.

For a filter, it does not match only one servlet as the servlet does. Because the set of filters is a chain, there will only be different processing sequence, and only one filter will not be selected. The processing sequence of the Filter is the same as that defined by the filter-mapping in web. xml.

Ii. url-pattern explanation

In the web. xml file, the following syntax is used to define the ing:

L. What starts with "/" and ends with "/*" is used for path ing.

2. the prefix "*." is used for extension ing.

3. "/" is used to define the default servlet ing.

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

Therefore, why does the "/*. action" such a seemingly normal matching look wrong? Because this match is a path ing and also an extension ing, the container cannot judge.


3. Configure multiple url mappings:

<filter>    <filter-name>LoginFilter</filter-name>    <filter-class>com.gsol.enigma.web.filter.user.LoginFilter</filter-class>  </filter>  <filter-mapping>    <filter-name>LoginFilter</filter-name>    <url-pattern>/GeneralManager</url-pattern>  </filter-mapping>  <filter-mapping>    <filter-name>LoginFilter</filter-name>    <url-pattern>*.action</url-pattern>  </filter-mapping>

Original article: http://www.cnblogs.com/mailingfeng/archive/2012/04/05/2432687.html

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.