URL matching for servlet and filter Url-pattern

Source: Internet
Author: User
Tags filter

Servlet and filter is commonly used in the development of Java-EE technology, easy to use, simple configuration, suitable for all ages.

It is estimated that most of the friends are directly configured to use, also did not care about the specific details, today encountered a problem, the Internet to check the specification of the servlet only found that the servlet and filter in the Url-pattern or some articles in the inside, summed up some things, put out for everyone's reference, Avoid problems and waste time.

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 , and found that/test exactly matched by Servleta, then call Servleta and 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?). 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:

The "/" and ending with "/*" are used to do path mapping.

Previous prefix "*." The beginning is used to do the extension mapping.

"is used to define the default servlet mapping.

The rest 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.

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.