Home  >  Article  >  Web Front-end  >  The url-pattern setting method and mapping rules of servlet and filter

The url-pattern setting method and mapping rules of servlet and filter

一个新手
一个新手Original
2017-09-20 10:03:482018browse



<span style='margin:0px; padding:0px; font-family:Arial,"Microsoft YaHei"; color:rgb(0,128,128); line-height:1.5!important'></span><p style='margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:Arial,"Microsoft YaHei"; line-height:19px; word-break:break-all; background-color:rgb(254,254,242)'><span style="margin:0px; padding:0px; font-family:verdana,sans-serif; line-height:21px">Servlet和filter是J2EE开发中常用的技术,使用方便,配置简单。servlet和filter中的url-pattern有一些文章在里面的,总结了一些东西,以免遇到问题又要浪费时间。 <br>   </span></p><p style='margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:Arial,"Microsoft YaHei"; line-height:19px; word-break:break-all; background-color:rgb(254,254,242)'><span style="margin:0px; padding:0px; font-family:verdana,sans-serif; line-height:21px">一,servlet容器对url的匹配过程: <br><br>当 一个请求发送到servlet容器的时候,容器先会将请求的url减去当前应用上下文的路径作为servlet的映射url,比如我访问的是 http://localhost/test/aaa.html,我的应用上下文是test,容器会将http://localhost/test去掉, 剩下的/aaa.html部分拿来做servlet的映射匹配。这个映射匹配过程是有顺序的,而且当有一个servlet匹配成功以后,就不会去理会剩下 的servlet了(filter不同,后文会提到)。其匹配规则和顺序如下: <br><br><span style='margin:0px; padding:0px; font-family:Arial,"Microsoft YaHei"'>1.     精确路径匹配。</span>例子:比如servletA 的url-pattern为 /test,servletB的url-pattern为 /* ,这个时候,如果我访问的url为http://localhost/test ,这个时候容器就会先进行精确路径匹配,发现/test正好被servletA精确匹配,那么就去调用servletA,也不会去理会其他的 servlet了。 <br><br><span style='margin:0px; padding:0px; font-family:Arial,"Microsoft YaHei"'>2.     最长路径匹配</span>。例子:servletA的url-pattern为/test/*,而servletB的url-pattern为/test/a/*,此 时访问http://localhost/test/a时,容器会选择路径最长的servlet来匹配,也就是这里的servletB。 <br><br><span style='margin:0px; padding:0px; font-family:Arial,"Microsoft YaHei"'>3.     扩展匹配</span>,如果url最后一段包含扩展,容器将会根据扩展选择合适的servlet。例子:servletA的url-pattern:*.action <br><br>4.     如果前面三条规则都没有找到一个servlet,容器会根据url选择对应的请求资源。如果应用定义了一个<span style='margin:0px; padding:0px; font-family:Arial,"Microsoft YaHei"'>default servlet</span>,则容器会将请求丢给default servlet(什么是default servlet?后面会讲)。 <br><br>     根据这个规则表,就能很清楚的知道servlet的匹配过程,所以定义servlet的时候也要考虑url-pattern的写法,以免出错。 <br><br>      对于filter,不会像servlet那样只匹配一个servlet,因为filter的集合是一个链,所以只会有处理的顺序不同,而不会出现只选择一 个filter。Filter的处理顺序和filter-mapping在web.xml中定义的顺序相同。 <br>   </span></p><p style='margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:Arial,"Microsoft YaHei"; line-height:19px; word-break:break-all; background-color:rgb(254,254,242)'><span style="margin:0px; padding:0px; font-family:verdana,sans-serif; line-height:21px">二,url-pattern详解 <br><br>         在web.xml文件中,以下语法用于定义映射: <br><br>l. 以<span style='margin:0px; padding:0px; font-family:Arial,"Microsoft YaHei"'>”/’开头</span>和<span style='margin:0px; padding:0px; font-family:Arial,"Microsoft YaHei"'>以”/*”结尾</span>的是用来做<span style='margin:0px; padding:0px; font-family:Arial,"Microsoft YaHei"'>路径映射</span>的。 <br><br>2. 以<span style='margin:0px; padding:0px; font-family:Arial,"Microsoft YaHei"'>前缀”*.”开头</span>的是用来做<span style='margin:0px; padding:0px; font-family:Arial,"Microsoft YaHei"'>扩展映射</span>的。 <br><br>3. <span style='margin:0px; padding:0px; font-family:Arial,"Microsoft YaHei"'>“/”</span> 是用来定义<span style='margin:0px; padding:0px; font-family:Arial,"Microsoft YaHei"'>default servlet映射</span>的。 <br><br>4. 剩下的都是用来定义<span style='margin:0px; padding:0px; font-family:Arial,"Microsoft YaHei"'>详细映射</span>的。比如: /aa/bb/cc.action <br><br>所以,为什么定义”/*.action”这样一个看起来很正常的匹配会错?因为<span style='margin:0px; padding:0px; font-family:Arial,"Microsoft YaHei"'>这个匹配即属于路径映射,也属于扩展映射,导致容器无法判断</span>。</span></p><p style='margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:Arial,"Microsoft YaHei"; line-height:19px; word-break:break-all; background-color:rgb(254,254,242)'><span style="margin:0px; padding:0px; font-family:verdana,sans-serif; line-height:21px">另外,关于url-pattern映射之后, request的servletContextPath , ServletPath , PathInfo 情况,可参照下面链接的文章</span></p> 
 1 servlet与filter的url-pattern设置方式: 
 2  
 3 1、精确匹配: 
 4 /directory/file1.jsp 
 5 /directory/file2.jsp 
 6 /directory/file3.jsp 
 7  
 8 2、目录匹配: 
 9 /directory/*
 10 
 11 3、扩展匹配:
 12 *.jsp
 13 
 14 注意:下面的不支持:
 15 /direcotry/*.jsp
 16 
 17 /和/*之间的区别:
 18 <url-pattern>/</url-pattern>: 会匹配到/login这样的路径型url,不会匹配到模式为*.jsp这样的后缀型url
 19 <url-pattern>/*</url-pattern>:会匹配所有url:路径型的和后缀型的url(包括/login , *.jsp , *.js 和 *.html 等)
 20 <url-pattern>/</url-pattern>: 甚至会造成The requested resource () is not available.

The above is the detailed content of The url-pattern setting method and mapping rules of servlet and filter. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn