Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple filters with same url mapping

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>
like image 443
mosaad Avatar asked Nov 15 '13 10:11

mosaad


People also ask

Can we have multiple filters in Web xml?

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.

What is URL pattern in filter-mapping?

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.

What is the use of filter-mapping in Web xml?

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.


2 Answers

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

like image 138
Suresh Atta Avatar answered Oct 05 '22 00:10

Suresh Atta


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

like image 35
Tetereou Mouhammad Abdourazak Avatar answered Oct 05 '22 01:10

Tetereou Mouhammad Abdourazak