Question

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>
Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top