Servlet and Filter the url matches the url-pattern

October 12, 2010

Servlet and filter is commonly used in J2EE development of the technology, easy to use, simple configuration, all ages.

Most of my friends are direct estimates, also did not care about any specific details, encountered a problem today, Internet search discovered the servlet specification, servlet and filter in the url-pattern or a number of articles in it, and summing up something, let out for your reference, to avoid trouble but also a waste of time.

1, servlet containers on the url in the matching process:

When a request is sent to the servlet container when the container will first request the url minus the current application context of the path as the servlet mapping url, such as my visit is http://localhost/test/aaa.html, my application context is the test, the container will http://localhost/test removed, the remaining / aaa.html part of the servlet mapping used as a match. The map matching process is a sequence, and when there is a servlet after a successful match, they will not care about the rest of the servlet of the (filter different from the text will be mentioned later). The matching rules and order as follows:

1. Exact path match. Example: For example servletA the url-pattern is / test, servletB the url-pattern to / bin / boot / dev / etc / home / lib / lost + found / media / misc / mnt / net / opt / proc / root / sbin / selinux / srv / sys / tmp / u01 / usr / var / vmware, this time, if I visit the url for the http://localhost/test, this time the precise path of the container will be the first match, find / test is just servletA exact match, then went to call servletA, not to ignore the other servlet.

2. Longest path match. Examples: servletA the url-pattern is / test / *, and servletB the url-pattern is / test / a / *, then visit http://localhost/test/a, the container will choose the longest path to servlet match, which was the servletB.

3. Extended match, the last paragraph if the url contains the expansion vessel will be based on expanding selection of suitable servlet. Examples: servletA the url-pattern: *. action

4. If the previous three rules do not find a servlet, the container will be selected according to the corresponding url requested resource. If the application defines a default servlet, the container will request threw default servlet (What is a default servlet? Speak later).

According to the rule table, we can clearly know the servlet the matching process, so the definition should be considered when the servlet url-pattern of the wording, to avoid mistakes.

For the filter, will not only match, as did a servlet servlet, because the filter set is a chain, so there will only be dealt with in a different order, but does not appear to select only one filter. Filter processing the order and filter-mapping defined in web.xml the same order.

Second, url-pattern Xiangjie

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

To “/ 'at the beginning and the” / * “at the end of the path is used for mapping.

With the prefix “*.” at the beginning is used for expansion of mapping.

“Is used to define the default servlet mapping.

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

So, why the definition of “/ *. action” such a match would be the wrong seems normal? Because it matches the path that is mapped, it is expanding map, leading to the container can not judge.

Recommended links:

MKV To VOB

MPEG To 3GP

Myself A Good Looking Word Text Box

3GPP To WMV

Get free blog up and running in minutes with Blogsome | Theme designs available here