ServletAPI の Filter を使うと、特定のURL(サーブレット)に対して前処理や後処理を追加することができます。
これから、フィルターで処理を追加する方法を書いていきます。
サーブレットのバージョンは以下の通りです。
動作確認のために、以下の製品を使用しました。
前処理と後処理を実装するクラスを作成します。
org/example/filter/SampleFilter.java
package org.example.filter;import java.io.IOException;import jakarta.servlet.Filter;import jakarta.servlet.FilterChain;import jakarta.servlet.ServletException;import jakarta.servlet.ServletRequest;import jakarta.servlet.ServletResponse;import jakarta.servlet.annotation.WebFilter;@WebFilter("/filter/receive")publicclass SampleFilterimplements Filter {@Overridepublicvoid doFilter( ServletRequest req, ServletResponse res, FilterChain chain)throws IOException, ServletException {// 前処理を実行 System.out.println("フィルターの前処理");// 次の処理(サーブレットかフィルター)を呼び出す chain.doFilter(req, res);// 後処理を実行 System.out.println("フィルターの後処理"); }}
@WebFilter で、フィルターが呼び出されるURLを指定します。URLを複数指定したい場合は、下のようにurlPatterns を使います。
@WebFilter( urlPatterns={"/filter/receive","/req/*"})
URLの形式ですが、ワイルドカードも使用可能です。
動作確認をする場合、以下のクラスを作成します。
org/example/filter/SampleServlet.java
package org.example.filter;import java.io.IOException;import jakarta.servlet.ServletException;import jakarta.servlet.annotation.WebServlet;import jakarta.servlet.http.HttpServlet;import jakarta.servlet.http.HttpServletRequest;import jakarta.servlet.http.HttpServletResponse;@WebServlet("/filter/receive")@SuppressWarnings("serial")publicclass SampleServletextends HttpServlet {publicvoid doGet(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException { System.out.println("サーブレットの処理"); }}
@WebServlet で、フィルターと同じURLを設定しています。
以下のコマンドでプロジェクトsample-servlet のディレクトリ階層を作成します。
mkdir sample-servletcd sample-servletmkdir src\main\javamkdir src\main\webapp\WEB-INF
作成したら、src/main/java 配下にフィルターとサーブレットのコードを置きます。
フォルダsample-servlet にpom.xml を作成します。
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>sample-servlet</artifactId> <version>1.0.0</version> <packaging>war</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>jakarta.servlet</groupId> <artifactId>jakarta.servlet-api</artifactId> <version>5.0.0</version> <scope>provided</scope> </dependency> </dependencies> <build> <finalName>${project.artifactId}</finalName> <plugins> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.4.0</version> </plugin> <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>11.0.15</version> <configuration> <scan>1</scan> </configuration> </plugin> </plugins> </build></project>サーブレットコンテナを起動できるように、build で Jetty Plugin を定義しています。
以下のコマンドでコンテナを起動します。
sample-servlet> mvn jetty:run
起動後にブラウザで以下の URL を開きます。
http://localhost:8080/filter/receive
以下の文字列が標準出力されます。
フィルターの前処理サーブレットの処理フィルターの後処理
引用をストックしました
引用するにはまずログインしてください
引用をストックできませんでした。再度お試しください
限定公開記事のため引用できません。