程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> JSP編程 >> 關於JSP >> JSP過濾器Filter配置過濾類型全部匯總

JSP過濾器Filter配置過濾類型全部匯總

編輯:關於JSP

一、配置方法

1 映射過濾應用程序中所有資源

<filter>
  <filter-name>loggerfilter</filter-name>
  <filter-class>myfilter.LoggerFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>loggerfilter</filter-name>
  <url-pattern>/*</url-pattern> //訪問當前主機,當前應用程序根下的所有文件包括多級子目錄下的所有文件,注意這裡*前有“/”
</filter-mapping>

2 過濾指定的類型文件資源

<filter>
  <filter-name>loggerfilter</filter-name>
  <filter-class>myfilter.LoggerFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>loggerfilter</filter-name>
  <url-pattern>*.html</url-pattern> //訪問當前主機,當前應用程序根目錄下的所有html文件,注意:*.html前沒有“/”,否則錯誤
</filter-mapping>

其中*.html要過濾jsp那麼就改*.html為*.jsp,但是注意沒有“/”斜槓。如果要同時過濾多種類型資源:

方法1 url-pattern 分開寫

<filter>
  <filter-name>loggerfilter</filter-name>
  <filter-class>myfilter.LoggerFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>loggerfilter</filter-name>
  <url-pattern>*.html</url-pattern>
</filter-mapping>
<filter-mapping>
  <filter-name>loggerfilter</filter-name>
  <url-pattern>*.jsp</url-pattern>//訪問當前主機,當前應用程序根目錄以所有及子目錄下的所有jsp文件
</filter-mapping>

方法2 將url-pattern合並

<filter>
  <filter-name>loggerfilter</filter-name>
  <filter-class>myfilter.LoggerFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>loggerfilter</filter-name>
  <url-pattern>*.html;*.jsp</url-pattern>
</filter-mapping>//*.html;*.jsp兩類型之間用分號;間隔

3 過濾指定的目錄下的所有文件

<filter>
  <filter-name>loggerfilter</filter-name>
  <filter-class>myfilter.LoggerFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>loggerfilter</filter-name>
  <url-pattern>/folder_name/*</url-pattern>//訪問當前主機,當前應用程序根目錄下的folder_name子目錄(可是多級子目錄)下所有文件
</filter-mapping>

4 過濾指定的servlet

<filter>
  <filter-name>loggerfilter</filter-name>
  <filter-class>myfilter.LoggerFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>loggerfilter</filter-name>
  <servlet-name>loggerservlet</servlet-name>
</filter-mapping>
<servlet>
  <servlet-name>loggerservlet</servlet-name>
  <servlet-class>myfilter.LoggerServlet</servlet-class>
</servlet>

5 過濾指定文件(即單一文件)

<filter>
  <filter-name>loggerfilter</filter-name>
  <filter-class>myfilter.LoggerFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>loggerfilter</filter-name>
  <url-pattern>/simplefilter.html</url-pattern>
</filter-mapping>

6 過濾指定目錄下的指定類型的所有文件

這種情況下在web.xml中無法一次性配置完成,需要結合filter的實現類

首先在web.xml中配置過濾指定目錄下的所有文件

<filter>
  <filter-name>loggerfilter</filter-name>
  <filter-class>myfilter.LoggerFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>loggerfilter</filter-name>
  <url-pattern>/dir_name/*</url-pattern>
</filter-mapping>

然後在filter的實現類中的doFilter方法中獲取請求的uri或者url,判斷uri或者url中是否包含指定文件類型的字符串,決定是否過濾

//獲得用戶請求的uri
String uri = request.getRequestURI();
if(uri.contains(".jsp")||uri.contains(".html"))
System.out.println("開始過濾"+url);

7 過濾指定目錄下指定類型的單一文件

<filter>
  <filter-name>loggerfilter</filter-name>
  <filter-class>myfilter.LoggerFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>loggerfilter</filter-name>
  <url-pattern>/dir_name/index.jsp</url-pattern>
</filter-mapping>

以上都要注意是否有斜槓“/”

二、實例

不緩存文件

<filter>
  <filter-name>NoCache</filter-name>
  <filter-class>com.sys.common.Filters.ResponseHeaderFilter
    </filter-class>
  <init-param>
   <param-name>Cache-Control</param-name>
   <param-value>no-cache, must-revalidate</param-value>
  </init-param>
 </filter>

緩存文件一周

 <filter>
  <filter-name>CacheForWeek</filter-name>
  <filter-class>com.sys.common.Filters.ResponseHeaderFilter
    </filter-class>
  <init-param>
   <param-name>Cache-Control</param-name>
   <param-value>max-age=604800, public</param-value>
  </init-param>
 </filter>
<filter-mapping>
  <filter-name>CacheForWeek</filter-name>
  <url-pattern>/images/*</url-pattern>
 </filter-mapping>
 <filter-mapping>
  <filter-name>CacheForWeek</filter-name>
  <url-pattern>*.js</url-pattern>
 </filter-mapping>
 <filter-mapping>
  <filter-name>CacheForWeek</filter-name>
  <url-pattern>*.css</url-pattern>
 </filter-mapping>

自定義的filter:

public class ResponseHeaderFilter implements Filter {
  FilterConfig fc;
  public void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) throws IOException, ServletException {
    HttpServletResponse response = (HttpServletResponse) res;
    for (Enumeration e = fc.getInitParameterNames(); e.hasMoreElements();) {
      String headerName = (String) e.nextElement();
      response.addHeader(headerName, fc.getInitParameter(headerName));
    }
    chain.doFilter(req, response);
  }

  public void init(FilterConfig filterConfig) {
    this.fc = filterConfig;
  }

  public void destroy() {
    this.fc = null;
  }

}

以上這篇JSP過濾器Filter配置過濾類型全部匯總就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved