1 つの具体的な URL を除くすべての URL に具体的なフィルターを適用します (つまり、/*
を除く/specialpath
)。
それを行う可能性はありますか?
サンプルコード:
<filter>
<filter-name>SomeFilter</filter-name>
<filter-class>org.somproject.AFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SomeFilter</filter-name>
<url-pattern>/*</url-pattern> <!-- the question is: how to modify this line? -->
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
ベストアンサー1
標準のサーブレットAPIはこの機能をサポートしていません。このためには、次のような書き換えURLフィルタを使用することをお勧めします。タッキーのもの(これは Apache HTTPD の と非常によく似ています)、またはをリッスンする Filter の メソッドmod_rewrite
にチェックを追加します。doFilter()
/*
String path = ((HttpServletRequest) request).getRequestURI();
if (path.startsWith("/specialpath/")) {
chain.doFilter(request, response); // Just continue chain.
} else {
// Do your business stuff here for all paths other than /specialpath.
}
必要に応じて、無視するパスをinit-param
フィルターの として指定して、フィルター内で制御できるようにすることができますweb.xml
。フィルター内では次のようにして取得できます。
private String pathToBeIgnored;
public void init(FilterConfig config) {
pathToBeIgnored = config.getInitParameter("pathToBeIgnored");
}
フィルターがサードパーティ API の一部であり、変更できない場合は、より具体的な にマップしますurl-pattern
。たとえば、サードパーティ フィルターに一致するパスに転送する/otherfilterpath/*
新しいフィルターを作成します。/*
String path = ((HttpServletRequest) request).getRequestURI();
if (path.startsWith("/specialpath/")) {
chain.doFilter(request, response); // Just continue chain.
} else {
request.getRequestDispatcher("/otherfilterpath" + path).forward(request, response);
}
このフィルターが無限ループで自身を呼び出すのを回避するには、フィルターが のみ でリッスン (ディスパッチ)REQUEST
し、サードパーティ フィルターがFORWARD
のみ でオンになるようにする必要があります。