Mapping rules for Url-pattern in Web.xml __web

Source: Internet
Author: User

Servlet and filter are commonly used in the development of Java-EE, easy to use and simple to configure. Url-pattern in the servlet and filter there are some articles in it that summarize something to avoid problems and waste time.

First, the servlet container to match the URL process:

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 , and found that/test exactly matched by Servleta, then call Servleta and ignore the other servlet.

2. Longest 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. Extended Match, 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 adefault Servlet, the container throws the request to the default servlet (what is the default servlet). will be speaking later).

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 it wrong to define "/*.action" as a seemingly normal match. Because this match belongs to the path map, it is also an extension map, causing the container to be unable to judge .


In addition, after the Url-pattern mapping, request Servletcontextpath, Servletpath, pathinfo situation, can refer to the link below the article

http://blog.csdn.net/cooljia/article/details/187882

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.