2012-02-22 58 views
3

如何使用僅用於servlet而不適用於jsp的過濾器?僅針對servlets調用的過濾器

 
url-patterns :
/* - this makes the container to call the filter for servlets as well as jsp. *.jsp - this makes the container to call the filter only for jsp.

有它調用過濾器只對servlet任何方式..

回答

4

由於過濾器映射到URL和映射始終是「陽性」,即你不能說「除非網址是調用這個過濾器* .JSP)唯一的解決辦法是創造或者servlet或JSP特殊的URL。

例如您可以將所有的servlet映射到與*.do結尾的URL,例如create.dodelete.do

<servlet-mapping> 
    <servlet-name>Create Servlet</servlet-name> 
    <url-pattern>/create.do</url-pattern> 
</servlet-mapping> 
<servlet-mapping> 
    <servlet-name>Delete Servlet</servlet-name> 
    <url-pattern>/delete.do</url-pattern> 
</servlet-mapping> 

然後您可以創建過濾器並將其映射到*.do

<filter-mapping> 
    <filter-name>actionsFilter</filter-name> 
    <url-pattern>*.do</url-pattern> 
</filter-mapping> 

它將爲所有servlet工作(因爲它們映射到*.do),不會對JSP的工作(因爲它們映射到*.do)。

+0

這就是一個簡單的伎倆,我試過了,但它給出了一個404錯誤 – lee 2012-02-22 08:59:51

+0

它的工作沒有 「/」 ..日Thnx了很多.. – lee 2012-02-22 10:01:51

1

您可以通過添加一個不傳播到FilterChain的虛擬過濾器來實現此目的,即不在Dummy Filter中調用FilterChain.doFilter(),但包括用於jsp文件的requestdispatcher。

public class NOPDummyFilter implements Filter { 

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { 
     req.getRequestDispatcher(req.getServletContext().getContextPath() 
      .substring(req.getServletContext().getC‌​ontextPath().lastIndexOf('/') + 1)).include(request, response); 
    } 
    public void init(FilterConfig config) throws ServletException { 
    } 
    public void destroy() { 
    } 
} 

,並在web.xml:

<filter> 
    <filter-name>NOPDummyFilter</filter-name> 
    <filter-class>NOPDummyFilter</filter-class> 
</filter> 

<filter-mapping> 
    <filter-name>NOPDummyFilter</filter-name> 
    <url-pattern>*.jsp</url-pattern> 
</filter-mapping> 

<filter-mapping> 
    <filter-name>MyRealServletFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 

看到這樣的討論:http://www.coderanch.com/t/169859/java-Web-Component-SCWCD/certification/Filter-has-Exclude-url-pattern

希望這有助於。

+0

,但如果我不要調用chain.doFilter(),它不會調用在我的情況下是歡迎文件的jsp。所以它顯示一個空白頁。 – lee 2012-02-22 09:13:57

+0

你應該可以這樣做:'req.getRequestDispatcher(「/ WEB-INF/header.jsp」).include(request,response);'在doFilter()方法中使過濾器向前或包含一個RequestDispatcher爲JSP。但我對此並不百分之百肯定。 – fasseg 2012-02-22 09:36:06

+0

它不會工作,因爲它會調用所有的jsp的header.jsp .. :(.. – lee 2012-02-22 09:56:52

0

enter image description here

和代碼:

// Check if request goto a Servlet 
private boolean needFilter(HttpServletRequest request) { 
    // 
    // Servlet Url-pattern: /path/* 
    // 
    // => /path 
    String servletPath = request.getServletPath(); 
    // => /abc/mnp 
    String pathInfo = request.getPathInfo(); 

    String urlPattern = servletPath; 

    if (pathInfo != null) { 
     // => /path/* 
     urlPattern = servletPath + "/*"; 
    } 

    // Key: servletName. 
    // Value: ServletRegistration 
    Map<String, ? extends ServletRegistration> servletRegistrations = request.getServletContext() 
      .getServletRegistrations(); 

    // collection of all servlets in your webapp. 
     // containing *.jsp & *.jspx 
    Collection<? extends ServletRegistration> values = servletRegistrations.values(); 
    for (ServletRegistration sr : values) { 
     Collection<String> mappings = sr.getMappings(); 
     if (mappings.contains(urlPattern)) { 
      return true; 
     } 
    } 
    return false; 
}