Suraj Chandran
Suraj Chandran

Reputation: 24791

Can we confgure filters only for a subset of servlets

I have various different categories of servlets. Can I configure the filters in my web.xml such that each filter only applies for one of the categories of servlet.

Upvotes: 1

Views: 121

Answers (1)

BalusC
BalusC

Reputation: 1108632

You can map them on <servlet-name> instead of <url-pattern>.

<filter-mapping>
    <filter-name>yourFilterName</filter-name>
    <servlet-name>yourServlet1Name</servlet-name>
    <servlet-name>yourServlet2Name</servlet-name>
    <servlet-name>yourServlet3Name</servlet-name>
</filter-mapping>

The <servlet-name> must exactly match the same value as in <servlet> definition. This way the filter will be invoked whenever either of those servlets are to be invoked.

Upvotes: 6

Related Questions