Is it possible to use two filters that have the same url-mapping?
<filter>
<filter-name>TeeFilter</filter-name>
<filter-class>filter1r</filter-class>
</filter>
<filter-mapping>
<filter-name>TeeFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<display-name>CredentialsFilter</display-name>
<filter-name>CredentialsFilter</filter-name>
<filter-class>filter2</filter-class>
</filter>
<filter-mapping>
<filter-name>CredentialsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
We can have multiple filters for a single resource and we can create a chain of filters for a single resource in web. xml. We can create a Servlet Filter by implementing javax. servlet.
The url-pattern element of a servlet-mapping or a filter-mapping associates a filter or servlet with a set of URLs. When a request arrives, the container uses a simple procedure for matching the URL in the request with a url-pattern in the web. xml file.
The filter-mapping element maps a URL pattern or servlet name to an instance of a filter. The filter-mapping always contains a filter-name element and a url-pattern element. The filter-name element must match a filter-name defined in a filter element elsewhere in the web. xml file.
Yes. You can.
The order you placed in web.xml will execute.
So here,
First control goes to TeeFilter
and then to CredentialsFilter
.
And if you want to execute CredentialsFilter
first, change the order in web.xml
When using annotation ,it seems like it compares the two filters lexicographically.And then choose the one that comes first according to this comparison. Example I tried with two filters named Filter1.java and Filter2.java.Both filters have the same url-mapping. When I invoke the mapped url,Filter1.java executes first.But When I rename Filter1.java to Filter3.java. Then Filter2.java executes first. To be more clear. If you have 2 filters A and B.When using annotation to map the same url,A will always execute first. Can you try if you can achieve this result like me.Thanks.If not let me know
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With