Servlet - Filter配置方法
Servlet3.0からは下記2種類の配置方法がある。
1. Filterクラスに@WebFilterアノテーションを使う。(使用例)
2. 従来方法であるweb.xmlに記述する。
WebFilterの常用属性
属性 | 説明 |
---|---|
asyncSupported | Servletが非同期操作モードをサーポートするか |
dispatcherTypes | ASYNC, FORWARD, INCLUDE, ERROE, REQUESTのどのモードにFilterを適用するか |
displayName | Filterの表示名 |
initParames | Filterの初期化パラメータ |
servletName | どのservletに対してFilterを適用するか |
urlPatters/value | どのURL(二つの属性は同じである)にFilterを適用するか(/*ですべて) |
※アノテーション使用注意点は
1. web.xmlの<web-app>タグにmetadata-complete="true"を指定する必要がある。
2. web.xml中に対象Filterを配置しないこと。
web.xmlにFilterを配置 |
<web-app 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_3_1.xsd"
id="WebApp_ID" version="3.1">
<filter>
<filter-name>firstFilter</filter-name>
<filter-class>FirstFilter</filter-class>
<init-param>
<param-name>encode</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>firstFilter</filter-name>
<!-- すべてのURLに対してFilter適用 --> <url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
|