⑴ lt;filter-mapping>中與多個patter匹配的<url-pattern>應該怎麼寫
看一個例:
<filter>
<filter-name>edesignPhotoFilter</filter-name>
<display-name>EDesign Photo Filter</display-name>
<description>Disable Load photo from url</description>
<filter-class>edesign.filter.EDesignImageFilter</filter-class>
<init-param>
<param-name>paths</param-name>
<param-value>/photos/,/lphotos/,/userHome/, /attachments/</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>edesignPhotoFilter</filter-name>
<url-pattern>/photos/*.jpg</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>edesignPhotoFilter</filter-name>
<url-pattern>/lphotos/*.jpg</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>edesignPhotoFilter</filter-name>
<url-pattern>/userHome/*.jpg</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>edesignPhotoFilter</filter-name>
<url-pattern>/attachments/*.jpg</url-pattern>
</filter-mapping>
⑵ 關於Servlet的filter過濾URL的問題
在web。xml里配置一個filter 把要過濾的頁面放在一個文件夾下面 /文件夾名稱/*
⑶ chain.doFilter過濾器如果要過濾多個網頁但不是所有的網頁web.xml在配置的時候怎麼寫~~~
HttpServletRequest req = (HttpServletRequest )request;
HttpServletRequest res = (HttpServletRequest )response;
String s = req.get***url(); //----具體去試,就是每次的請求的地址
//一般都是「/項目名/***」
if(!s.equel*******("/項目名/***") && !s.equel*******("/項目名/***") ){
//如果不匹配給出的路徑,則放行
chain.doFilter(req, res);
}else{
return;
}
s.equel******* ----(最長的那一個,就是比較字元串)
在web.xml裡面把這個Filter配進去,放在其他的Filter前面
<filter-mapping>
<url-parme>/*</url-parme>
</filter-mapping>
/* 攔截所有的請求~~~~~~~~~~
都是手打,大概的在myeclipse都有提示的
這樣過濾了沒什麼好處,最好是設一個session,不然你過濾的網頁永遠都訪問不到了~~~~~~~~~~~
⑷ javaweb filter過濾掉 request請求帶有 add的請求怎麼寫過濾的url /*a
filter只能過濾兩種URL
Pattern
/xxxx/*或者*.,
不支持樓主這樣的模式,
實現過濾add請求,最好寫成/add/xxxx
⑸ JSP過濾器這樣設置過濾一個次級路徑下的所有URL
/sites/*這樣就可以了,如果這個文件夾是某個子目錄的話還需要添加它的父目錄;
下面內是些例子
<url-pattern>:指定和過濾器關聯容的URL,為」/*」表示所有URL;
例子1:單個過濾器配置:容器將其應用於所有接收的請求
<url-pattern>/*</url-pattern>
例子2:過濾器應用到特定目錄或資源(文件)的配置:此容器只有在接收到對/mydocs目錄中的資源的請求時才會應用該過濾器。
<url-pattern>/mydocs/*</url-pattern>
⑹ 寫了個filter,我需要過濾所有的servlet 應該怎麼寫 <url-pattern> 啊
配置的是訪問路徑,不是包路徑,就是瀏覽器請求的路徑:
例如:http://localhost:8080/mysite/user
若訪問這個路徑轉到com.item.servlets的servlet,就應設為
/user,明白了吧。
⑺ java過濾器不過濾某個貨某一些路徑
首先在web.xml 配置 參數
<param-name>noLoginPaths</param-name> 參數名可自取
<filter>
<filter-name>LoginFilter</filter-name>
<filter-class>com.imooc.filter.LoginFilter</filter-class>
<init-param>
<param-name>noLoginPaths</param-name>
<param-value>login.jsp;fail.jsp;LoginServlet</param-value>
</init-param>
<init-param>
<param-name>charset</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
然後在 Filter 中,創建一個FilterConfig 的引用, 這個引用在init中初始化,並且能夠通過它獲取<init-param>中的key和value ,
StringnoLoginPaths=config.getInitParameter("noLoginPaths");
然後通過判斷用戶請求的url 是否符合 noLoginPaths 中設置的不過濾選項,如果如何,就調用do.filter放行
關鍵代碼 :
if(noLoginPaths!=null){
String[]strArray=noLoginPaths.split(";");
for(inti=0;i<strArray.length;i++){
if(strArray[i]==null||"".equals(strArray[i]))continue;
if(request.getRequestURI().indexOf(strArray[i])!=-1){
arg2.doFilter(arg0,arg1);
return;
}
}
}
完整代碼:
importjava.io.IOException;
importjavax.servlet.Filter;
importjavax.servlet.FilterChain;
importjavax.servlet.FilterConfig;
importjavax.servlet.ServletException;
importjavax.servlet.ServletRequest;
importjavax.servlet.ServletResponse;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjavax.servlet.http.HttpSession;
{
privateFilterConfigconfig;
@Override
publicvoiddestroy(){
}
@Override
publicvoiddoFilter(ServletRequestarg0,ServletResponsearg1,FilterChainarg2)throwsIOException,ServletException{
HttpServletRequestrequest=(HttpServletRequest)arg0;
HttpServletResponseresponse=(HttpServletResponse)arg1;
HttpSessionsession=request.getSession();
StringnoLoginPaths=config.getInitParameter("noLoginPaths");
Stringcharset=config.getInitParameter("charset");
if(charset==null){
charset="UTF-8";
}
request.setCharacterEncoding(charset);
if(noLoginPaths!=null){
String[]strArray=noLoginPaths.split(";");
for(inti=0;i<strArray.length;i++){
if(strArray[i]==null||"".equals(strArray[i]))continue;
if(request.getRequestURI().indexOf(strArray[i])!=-1){
arg2.doFilter(arg0,arg1);
return;
}
}
}
if(session.getAttribute("username")!=null){
arg2.doFilter(arg0,arg1);
}else{
response.sendRedirect("login.jsp");
}
}
@Override
publicvoidinit(FilterConfigarg0)throwsServletException{
config=arg0;
}
}
⑻ Filter過濾器中指定過濾內容怎麼配置
你的問題好像是只需要Filter處理jsp的請求,只要改web.xml里Filter配置就可以。url-pattern別給/*,給/.jsp,如果需要回過濾多種文答件請求,可以再增加filter-mapping,每個的url-pattern對應一種文件。
<filter-mapping>
<filter-name></filter-name>
<url-pattern></url-pattern>
</filter-mapping>
⑼ filter過濾器url-pattern如何設定
filter的url-pattern的設置、多個url-pattern
當前報表採用如下filter處理ec的excel導出,並且這個filter被封裝好了的<filter><filter-name>eXtremeExport</filter-name><filter-class>org.extremecomponents.table.filter.ExportFilter</filter-class></filter><filter-mapping><filter-name>eXtremeExport</filter-name><url-pattern>*.report</url-pattern></filter-mapping>
有其它頁面也用到了ec,也需要導出excel,但是後綴以action結尾,且只有2個特別的url("action1.action"、"action2.action")需要導出excel,嘗試這么寫不行<filter-mapping><filter-name>eXtremeExport</filter-name><url-pattern>action1.action</url-pattern></filter-mapping>這么寫可以
<filter><filter-name>drpEcExportFilter</filter-name><filter-class>com.common.filter.DrpEcExportFilter</filter-class></filter><filter-mapping><filter-name>drpEcExportFilter</filter-name><url-pattern>*.action</url-pattern></filter-mapping><filter-mapping><filter-name>drpEcExportFilter</filter-name><url-pattern>*.multidimensionalreport</url-pattern></filter-mapping>但是不夠精確
於是採用繼承的方式共享.DrpEcExportFilter,而url的處理由自己控制<filter-name>drpEcExportFilter</filter-name><filter-class>com.common.filter.DrpEcExportFilter</filter-class></filter><filter-mapping><filter-name>drpEcExportFilter</filter-name><url-pattern>*.action</url-pattern></filter-mapping>
其中DrpEcExportFilter.java的代碼-------------------------------------------------------------------------------------------------------package com.common.filter;import java.io.IOException;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;import org.extremecomponents.table.filter.ExportFilter;
public class DrpEcExportFilter extends ExportFilter implements Filter {
public void destroy() { super.destroy();}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest hrequest=(HttpServletRequest)request; String requestURI = hrequest.getRequestURI(); if (StringUtils.isNotEmpty(requestURI) && (requestURI.indexOf("action1.action") != -1 || requestURI .indexOf("action2.action") != -1)) { super.doFilter(request, response, chain); } else { chain.doFilter(request, response); }}
public void init(FilterConfig config) throws ServletException { super.init(config);}
}
以上是本人福沃德濾器回答,有需要請搜索福沃德濾器。
⑽ filter實現怎樣實現多個IP過濾
如果你在init-param中定義IP列表的話,可以用以下這種形式:
<param-value>192.168.0.68,192.168.0.13,192.168.0.44</param-value>,在doFilter方法裡面判斷remoteIP是否包含在這個filterIP字元串裡面。
這種做法的缺點是每次更新列表,WEB應用程序都要重新啟動,影響程序正常使用。而且,如果你的IP列表很大的話,web.xml文件會變得很難看。
另外一種常見的做法是,把IP列表寫到單獨一個文件或者是資料庫裡面,在程序啟動時,通過servlet載入到servletContext裡面。這樣你更新列表通過另外一個jsp或serlvet就可以,不需要重新啟動。