1. 程式人生 > >讀取web.xml中設定的引數

讀取web.xml中設定的引數

以獲取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的介面

 獲取的結果:

通過字元竄的分割函式拿出多個值,然後就進行你的操作了。