2011-12-20 67 views
29

我正在寫一個過濾器來完成特定的任務,但我無法爲我的過濾器設置特定的網址格式。我的過濾器映射如下:正如我此前的預期我們可以在web.xml中使用正則表達式嗎?

<web.xml> 
    <filter> 
    <filter-name>myFilter</filter-name> 
    <filter-class>test.MyFilter</filter-class> 
    </filter> 

    <filter-mapping> 
    <filter-name>myFilter</filter-name> 
    <url-pattern>/org/test/*/keys/*</url-pattern> 
    </filter-mapping> 
</web-app> 

我的url-pattern [ /org/test/ * /keys/ * ]不工作。

我打電話從瀏覽器的網址,如:

http://localhost:8080/myapp/org/test/SuperAdminReport/keys/superAdminReport.jsp 
http://localhost:8080/myapp/org/test/Adminreport/keys/adminReport.jsp 
http://localhost:8080/myapp/org/test/OtherReport/keys/otherReport.jsp 

所以對於過濾模式上面的網址應該匹配。我怎樣才能做到這一點?

+3

據我所知,您只能在urlpattern中使用特定文件夾的內容('path/to/my/folder/*')或特定擴展名('* .do')。 – bdares 2011-12-20 04:34:46

+0

請參閱[本問題]中接受的答案(http://stackoverflow.com/q/26732/922954) – aishwarya 2011-12-20 04:39:55

回答

6

你的網址不會工作,因爲它不是一個有效的網址模式。您可以參考下面的回覆

URL spec

0

web.xml的servlet映射,您只能在startend應用通配符,如果你嘗試應用通配符之間的映射不會被採摘。

3

這將無法正常工作,不能在那裏使用正則表達式。嘗試使用這個網址爲您的應用程序:

http://localhost:8080/myapp/org/test/keys/SuperAdminReport/superAdminReport.jsp 
http://localhost:8080/myapp/org/test/keys/Adminreport/adminReport.jsp 
http://localhost:8080/myapp/org/test/keys/OtherReport/otherReport.jsp 

而且這個配置你的web.xml:

<filter-mapping> 
    <filter-name>myFilter</filter-name> 
    <url-pattern>/org/test/keys/*</url-pattern> 
</filter-mapping> 
35

不,你不能使用正則表達式那裏。根據Java Servlet規範V2.4(部分srv.11.1),則URL路徑被解釋如下:

  • 的字符串用「/」字符開始並以結束一個「/ *」後綴用於路徑映射。
  • 以'*。'開頭的字符串被用作擴展映射。
  • 只包含'/'字符的字符串表示應用程序的「默認」servlet。在這種情況下,servlet路徑是 請求URI減去環境路徑,路徑信息爲空。
  • 所有其他字符串僅用於精確匹配。

不允許使用正則表達式。甚至沒有複雜的通配符。

0

以前的回答是正確的,因爲url模式只能以通配符開始或結束,因此不能使用正則表達式的真正威力。

不過,我已經通過創建一個簡單的默認篩選器,攔截所有請求,過濾器包含正則表達式,以確定進一步的邏輯是否應該應用於解決以往項目中這個問題。我發現這種方法幾乎沒有性能下降。

下面是一個簡單的例子,可以通過將正則表達式模式移動到過濾器屬性來增強。

在網絡中過濾配置。XML:

<filter> 
    <filter-name>SampleFilter</filter-name> 
    <filter-class>org.test.SampleFilter</filter-class> 
</filter> 
<filter-mapping> 
    <filter-name>SampleFilter</filter-name> 
    <servlet-name>SampleServlet</servlet-name> 
</filter-mapping> 
<servlet-mapping> 
    <servlet-name>SampleServlet</servlet-name> 
    <url-pattern>/*</url-pattern> 
</servlet-mapping> 

,可以使用任何正則表達式(對不起,使用Spring的OncePerRequestFilter父類)基本濾波器的實現:

public class SampleFilter extends OncePerRequestFilter { 

    @Override 
    final protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { 
     if (applyLogic(request)) { 
      //Execute desired logic; 
     } 

     filterChain.doFilter(request, response); 
    } 

    protected boolean applyLogic(HttpServletRequest request) { 
     String path = StringUtils.removeStart(request.getRequestURI(), request.getContextPath()); 

     return PatternMatchUtils.simpleMatch(new String[] { "/login" }, path); 
    } 
} 
8

正如其他人所指出的,沒有辦法用正則表達式匹配過濾僅使用基本的servlet過濾器功能。 servlet規範很可能被寫成允許高效地匹配url的方式,並且因爲servlet早於java中正則表達式的可用性(正則表達式到達了java 1.4)。

正則表達式可能會少高性能的(不,我還沒有基準),但如果你沒有強烈處理時間的限制,並希望爲便於配置的貿易表現,你可以做這樣的:

在你的過濾器:

private String subPathFilter = ".*"; 
private Pattern pattern; 

    @Override 
    public void init(FilterConfig filterConfig) throws ServletException { 
    String subPathFilter = filterConfig.getInitParameter("subPathFilter"); 
    if (subPathFilter != null) { 
     this.subPathFilter = subPathFilter; 
    } 
    pattern = Pattern.compile(this.subPathFilter); 
    } 

    public static String getFullURL(HttpServletRequest request) { 
    // Implement this if you want to match query parameters, otherwise 
    // servletRequest.getRequestURI() or servletRequest.getRequestURL 
    // should be good enough. Also you may want to handle URL decoding here. 
    } 

    @Override 
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { 
    Matcher m = pattern.matcher(getFullURL((HttpServletRequest) servletRequest)); 
    if (m.matches()) { 

     // filter stuff here. 

    } 
    } 

然後在web.xml中...

<filter> 
    <filter-name>MyFiltersName</filter-name> 
    <filter-class>com.konadsc.servlet.MyFilter</filter-class> 
    <init-param> 
     <param-name>subPathFilter</param-name> 
     <param-value>/org/test/[^/]+/keys/.*</param-value> 
    </init-param> 
    </filter> 
    <filter-mapping> 
    <filter-name>MyFiltersName</filter-name> 
    <url-pattern>/*</url-pattern> 
    </filter-mapping> 

通過反轉doFilter()中的if語句來排除匹配(或者爲排除模式添加另一個初始參數,這留給練習讀者)有時候也很方便。

相關問題