問題描述
是否可以在 J2EE - 過濾器中使用通配符? (Is it possible to use wildcards within J2EE - fitlers?)
I would like to apply a filter to severl url endings. The next configuraiton seems to work.
<filter>
<filter-name>LanguageFilter</filter-name>
<filter-class>filters.LanguageFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LanguageFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>LanguageFilter</filter-name>
<url-pattern>*.xml</url-pattern>
</filter-mapping>
Originally I asked if it was possible to use wildcards such as:
<url-pattern>*.do|*.xml</url-pattern>
But it does not seem to be possible.
參考解法
方法 1:
There is no notion of such an operator in the standard, so you need two mappings. The specification of the mappings is available in the servlet specification. See this article for a link and section.
方法 2:
You could cheat a bit, and map /. to your filter class, then have your Filter Implementation take a parameter where .do|.xml are parsed and though your filter, technically is able to see too much, it does a simply check on whether or not you've given it an additional test, does the test, and either filter's or noops.
(by Sergio del Amo、Alexander Torstling、Nathan Feger)