filter(二) filter中<url-pattern>的匹配规则(转)

下面的代码摘自tomcat源码,用来匹配<filter-mapping>中的<url-pattern>,相信比文字更容易理解:
java 代码
 
  1. /** 
  2.      * Return <code>true</code> if the context-relative request path 
  3.      * matches the requirements of the specified filter mapping; 
  4.      * otherwise, return <code>null</code>. 
  5.      * 
  6.      * @param filterMap Filter mapping being checked 
  7.      * @param requestPath Context-relative request path of this request 
  8.      */  
  9.     private boolean matchFiltersURL(FilterMap filterMap, String requestPath) {  
  10.   
  11.         if (requestPath == null)  
  12.             return (false);  
  13.   
  14.         // Match on context relative request path  
  15.         String testPath = filterMap.getURLPattern();  
  16.         if (testPath == null)  
  17.             return (false);  
  18.   
  19.         // Case 1 - Exact Match  
  20.         if (testPath.equals(requestPath))  
  21.             return (true);  
  22.   
  23.         // Case 2 - Path Match ("/.../*")  
  24.         if (testPath.equals("/*"))  
  25.             return (true);  
  26.         if (testPath.endsWith("/*")) {  
  27.             if (testPath.regionMatches(0, requestPath, 0,   
  28.                                        testPath.length() - 2)) {  
  29.                 if (requestPath.length() == (testPath.length() - 2)) {  
  30.                     return (true);  
  31.                 } else if ('/' == requestPath.charAt(testPath.length() - 2)) {  
  32.                     return (true);  
  33.                 }  
  34.             }  
  35.             return (false);  
  36.         }  
  37.   
  38.         // Case 3 - Extension Match  
  39.         if (testPath.startsWith("*.")) {  
  40.             int slash = requestPath.lastIndexOf('/');  
  41.             int period = requestPath.lastIndexOf('.');  
  42.             if ((slash >= 0) && (period > slash)   
  43.                 && (period != requestPath.length() - 1)  
  44.                 && ((requestPath.length() - period)   
  45.                     == (testPath.length() - 1))) {  
  46.                 return (testPath.regionMatches(2, requestPath, period + 1,  
  47.                                                testPath.length() - 2));  
  48.             }  
  49.         }  
  50.   
  51.         // Case 4 - "Default" Match  
  52.         return (false); // NOTE - Not relevant for selecting filters  
  53.   
  54.     }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值