以獲取Filer元素里設置的參數為例
先在web.xml文件中配置如下
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <filter> <!--定義過濾器--> <filter-name>filter</filter-name> <!--這個自定義類 在項目中的某個包中--> <filter-class >com.ou.book.filter.UrlFilter</filter-class> <!--設置默認的參數--> <init-param> <param-name>value</param-name> <param-value>nihao,wula,haha,heihei</param-value> </init-param> </filter> <filter-mapping> <!--使用上面定義的filter過濾器攔截--> <filter-name>filter</filter-name> <!-- /* -> 攔截所有請求 --> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
然后再用自定義的UrlFilter類讀取出設置的默認參數。
實現javax.servlet 中的Filter的接口
獲取的結果:
通過字符竄的分割函數拿出多個值,然后就進行你的操作了。