Detailed description of servlet url-pattern matching rules

Reprinted from: http://www.cnblogs.com/51kata/p/5152400.html

 

 

I. Overview

When using servlet or Filter to match url requests, the key point is the matching rules, but the matching rules in the servlet container are neither simple wildcards nor regular expressions, but their own rules, which are easy to confuse . This article will give a detailed example. The following instructions are all verified in the tomcat server.

Let's first introduce the concept of matching, the example code above. In the web.xml file of an app (such as myapp), there is the following information:

copy code
<servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>com.nau.MyServlet</servlet-class>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>xxxxxx</url-pattern>
   <url-pattern>yyyyyyy</url-pattern>
  </servlet-mapping>
copy code

In the above configuration information, the <servlet> tag first configures and declares a servlet, including the name of the servlet and the corresponding java class name.
The <servlet-mapping> tag declares the matching rule corresponding to the servlet, and each <url-pattern> tag represents one matching rule.

When the browser initiates a url request, when the request is sent to the servlet container, the container first takes the requested url minus the path of the current application context as the servlet mapping url, for example, the url is http://10.43.11.143/myapp /kata/detail.html, whose application context is myapp, the container will remove http://10.43.11.143/myapp, and the remaining /kata/detail.html part will be used for servlet mapping matching. This mapping matching process has a priority order (the specific priority order rules will be introduced later), and when a servlet is successfully matched, the remaining servlets will not be ignored.

Note that the matching rule of Filter is the same as that of servlet, but 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. 

Below we describe the various matching rules in detail

2. Exact match

The items configured in <url-pattern> must exactly match the url.

For example, the configuration information is as follows:

copy code
<servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/kata/detail.html</url-pattern>
    <url-pattern>/demo.html</url-pattern>
    <url-pattern>/table</url-pattern>
</servlet-mapping>
copy code

当在浏览器中输入如下几种url时,都会被匹配到该servlet
http://10.43.11.143/myapp/kata/detail.html
http://10.43.11.143/myapp/demo.html
http://10.43.11.143/myapp/table

注意:

http://10.43.11.143/myapp/table/ 是非法的url,不会被当作http://10.43.11.143/myapp/table识别

另外上述url后面可以跟任意的查询条件,都会被匹配,如

http://10.43.11.143/myapp/table?hello 这个请求就会被匹配到MyServlet。

 

三、扩展名匹配

如果匹配规则如下

<servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>*.jsp</url-pattern>
</servlet-mapping>

则任何扩展名为jsp(文件名和路径任意)的url请求都会匹配,比如下面的url都会被匹配
http://10.43.11.143/myapp/demo.jsp
http://10.43.11.143/myapp/test.jsp

 

四、路径匹配

如果匹配规则如下

<servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/kata/*</url-pattern>
</servlet-mapping>

则请求的ulr只要前面(myapp之后)的路径是/kata,而后面的路径可以任意。比如下面的url都会被匹配。
http://10.43.11.143/myapp/kata/demo.html
http://10.43.11.143/myapp/kata/test.jsp
http://10.43.11.143/myapp/kata/test/detail.html

http://10.43.11.143/myapp/kata/action

http://10.43.11.143/myapp/kata/action/

注意:路径和扩展名匹配无法同时设置,比如下面的三个<url-pattern>都是非法的,如果设置,启动tomcat服务器会报错。

<url-pattern>/kata/*.jsp</url-pattern>

<url-pattern>/*.jsp</url-pattern>

<url-pattern>he*.jsp</url-pattern>

另外注意:<url-pattern>/aa/*/bb</url-pattern>
这个是精确匹配,url必须是 /aa/*/bb,这里的*不是通配的含义

 

五、匹配任意的url

如果<url-pattern>配置成如下两种的任意一种

<url-pattern>/</url-pattern>

<url-pattern>/*</url-pattern>

则所有的url都可以被匹配上。其中/*是路径匹配,只是路径就是/。

 

六、优先顺序

当一个url与多个servlet的匹配规则可以匹配时,则按照 “ 精确路径 > 最长路径>扩展名”这样的优先级匹配到对应的servlet。举例如下:

例1:比如servletA 的url-pattern为 /test,servletB的url-pattern为 /* ,这个时候,如果我访问的url为http://localhost/test ,这个时候容器就会先进行精确路径匹配,发现/test正好被servletA精确匹配,那么就去调用servletA,不会去管servletB。

例2:比如servletA的url-pattern为/test/*,而servletB的url-pattern为/test/a/*,此时访问http://localhost/test/a时,容器会选择路径最长的servlet来匹配,也就是这里的servletB。 

Example 3: For example , the url-pattern of servletA: *.action, and the url-pattern of servletB is /*. At this time, if the url I visit is http://localhost/test.action, the container will take the path first. match, instead of matching the extension, so that servletB is called.

 

7. Summary

In this article, we have introduced the matching rules of servlet in detail. In general, it is divided into three matching methods: exact, path and extension, and introduces priority.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326876786&siteId=291194637