ApplicationFilterFactory:
/**
* Return <code>true</code> if the context-relative request path
* matches the requirements of the specified filter mapping;
* otherwise, return <code>false</code>.
*
* @param testPath URL mapping being checked
* @param requestPath Context-relative request path of this request
*/
private boolean matchFiltersURL(String testPath, String requestPath) {
if (testPath == null)
return (false);
// Case 1 - Exact Match
if (testPath.equals(requestPath))
return (true);
// Case 2 - Path Match ("/.../*")
if (testPath.equals("/*"))
return (true);
if (testPath.endsWith("/*")) {
if (testPath.regionMatches(0, requestPath, 0,
testPath.length() - 2)) {
if (requestPath.length() == (testPath.length() - 2)) {
return (true);
} else if ('/' == requestPath.charAt(testPath.length() - 2)) {
return (true);
}
}
return (false);
}
// Case 3 - Extension Match
if (testPath.startsWith("*.")) {
int slash = requestPath.lastIndexOf('/');
int period = requestPath.lastIndexOf('.');
if ((slash >= 0) && (period > slash)
&& (period != requestPath.length() - 1)
&& ((requestPath.length() - period)
== (testPath.length() - 1))) {
return (testPath.regionMatches(2, requestPath, period + 1,
testPath.length() - 2));
}
}
// Case 4 - "Default" Match
return (false); // NOTE - Not relevant for selecting filters
}
转:http://foxty.iteye.com/blog/39332
Servlet和Filter的url匹配以及url-pattern详解<o:p></o:p>
Servlet和filter是J2EE开发中常用的技术,使用方便,配置简单,老少皆宜。估计大多数朋友都是直接配置用,也没有关心过具体的细节,今天遇到一个问题,上网查了servlet的规范才发现,servlet和filter中的url-pattern还是有一些文章在里面的,总结了一些东西,放出来供大家参考,以免遇到问题又要浪费时间。<o:p></o:p>
一,servlet容器对url的匹配过程:<o:p></o:p>
<o:p></o:p>
当一个请求发送到servlet容器的时候,容器先会将请求的url减去当前应用上下文的路径作为servlet的映射url,比如我访问的是http://localhost/test/aaa.html,我的应用上下文是test,容器会将http://localhost/test去掉,剩下的/aaa.html部分拿来做servlet的映射匹配。这个映射匹配过程是有顺序的,而且当有一个servlet匹配成功以后,就不会去理会剩下的servlet了(filter不同,后文会提到)。其匹配规则和顺序如下:<o:p></o:p>
1. 精确路径匹配。例子:比如servletA 的url-pattern为 /test,servletB的url-pattern为 /* ,这个时候,如果我访问的url为http://localhost/test ,这个时候容器就会先 进行精确路径匹配,发现/test正好被servletA精确匹配,那么就去调用servletA,也不会去理会其他的servlet了。<o:p></o:p>
2. 最长路径匹配。例子:servletA的url-pattern为/test/*,而servletB的url-pattern为/test/a/*,此时访问http://localhost/test/a时,容器会选择路径最长的servlet来匹配,也就是这里的servletB。<o:p></o:p>
3. 扩展匹配,如果url最后一段包含扩展,容器将会根据扩展选择合适的servlet。例子:servletA的url-pattern:*.action<o:p></o:p>
4. 如果前面三条规则都没有找到一个servlet,容器会根据url选择对应的请求资源。如果应用定义了一个default servlet,则容器会将请求丢给default servlet(什么是default servlet?后面会讲)。<o:p></o:p>
根据这个规则表,就能很清楚的知道servlet的匹配过程,所以定义servlet的时候也要考虑url-pattern的写法,以免出错。<o:p></o:p>
对于filter,不会像servlet那样只匹配一个servlet,因为filter的集合是一个链,所以只会有处理的顺序不同,而不会出现只选择一个filter。Filter的处理顺序和filter-mapping在web.xml中定义的顺序相同。<o:p></o:p>
二,url-pattern详解<o:p></o:p>
在web.xml文件中,以下语法用于定义映射:
l 以”/’开头和以”/*”结尾的是用来做路径映射的。
l 以前缀”*.”开头的是用来做扩展映射的。
l “/” 是用来定义default servlet映射的。
l 剩下的都是用来定义详细映射的。比如: /aa/bb/cc.action
所以,为什么定义”/*.action”这样一个看起来很正常的匹配会错?因为这个匹配即属于路径映射,也属于扩展映射,导致容器无法判断。
分享到:
相关推荐
在利用servlet或Filter进行url请求的匹配时,很关键的一点就是匹配规则。这篇文章主要介绍了servlet的url-pattern匹配规则详细描述(小结),非常具有实用价值,需要的朋友可以参考下
一个页面请求根据url-pattern只匹配一个最佳的Servlet,而根据web.xml的filter-mapping标签的先后顺序,将满足要求的一个或者多个过滤器生成一个FilterChain 2.url-pattern配置 配置web.xml文件,用于定义映射的...
<url-pattern>*.do</url-pattern> </filter-mapping> <servlet> <servlet-name>ideawu</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load...
jsp servlet中的过滤器Filter配置总结 1。如果要映射过滤应用程序中所有资源: <filter> <filter-name>loggerfilter</filter-name> <filter-class>myfilter.... <url-pattern>/*</url-pattern> </filter-mapping>
@Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { ... <url-pattern>/jsp/*</url-pattern> </filter-mapping>
<url-pattern>④/ helloworld</url-pattern> 这里的”/ ”必须带!!!! </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> 把servlet-name等的servlet...
name> <filter-class> org.apache.struts2.dispatcher.ActionContextCleanUp </filter-class> </filter> <filter-mapping> <filter-name>struts-cleanup </filter-name> <url-pattern>/* </url-pattern> </filter-...
如果访问的路径中截取的内容和filter-mapping中的url-pattern相同,就会拦截用户的请求,然后到对应的filter类中,自动调用doFilter方法. 拦截之后的处理都有该法决定. (与servlet的区别: 调用doPost/doGet) .
<url-pattern>/*</url-pattern> </filter-mapping> <listener-class>com.vere.mvc.listener.VereMVCContextListener</listener-class> 新建一个TestAction 类 test/src -> com.test.action.TestAction ...
<url-pattern>*.jsf</url-pattern> </servlet-mapping> <filter> <filter-name>ExtensionsFilter</filter-name> <filter-class> org.apache.myfaces.component.html.util.ExtensionsFilter </filter-class>...
<url-pattern>/*</url-pattern> </filter-mapping> <listener-class>com.vere.mvc.listener.VereMVCContextListener</listener-class> 6.新建一个TestAction 类 test/src -> com.test.action.TestAction ...
package com.tarena.dingdang.filter; 02 03 import java.io.IOException; 04 import java.util.Enumeration; ...06 import javax.servlet.Filter;...78 <url-pattern>/*</url-pattern> 79 </filter-mapping>
<url-pattern>*.do</url-pattern> </servlet-mapping> </web-app> dispatcher-servlet.xml文件代码 <?xml version="1.0" encoding="UTF-8"?> xmlns:aop="http://www.springframework.org/schema/aop" ...
31 <url-pattern>*.do</url-pattern> 32 </filter-mapping> 33 34 <servlet> 35 <servlet-name>ideawu</servlet-name> 36 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 37 ...
<url-pattern>/*</url-pattern> </filter-mapping> 在web.xml配置spring <!-- 配置spring的配置文件的位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:...
<url-pattern>/admin/*</url-pattern> 如何处理登录问题? ①方式1 login.jsp放在WEB-INF /admin/user/toLogin->login.jsp /admin/user/login->点击登录时的处理 在过滤器中,if(url.indexOf("/...
servlet-api-provision B.1.1.2. path-type B.1.1.3. lowercase-comparisons B.1.1.4. realm B.1.1.5. entry-point-ref B.1.1.6. access-decision-manager-ref B.1.1.7. access-denied-page B.1.1.8....
6. <property name="url" value="jdbc:oracle:thin:@localhost:1521:ora9i"/> 7. 8. 9. 10. !-- Hibernate会话工厂配置 //--> 11. 12. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean...
servlet-api-provision B.1.1.2. path-type B.1.1.3. lowercase-comparisons B.1.1.4. realm B.1.1.5. entry-point-ref B.1.1.6. access-decision-manager-ref B.1.1.7. access-denied-page B.1.1.8....
servlet-api-provision B.1.1.2. path-type B.1.1.3. lowercase-comparisons B.1.1.4. realm B.1.1.5. entry-point-ref B.1.1.6. access-decision-manager-ref B.1.1.7. access-denied-page B.1.1.8. once-...