I know how to filter a Servlet but I don't know how can I apply more than one filter for a servlet. I have attached an image for the same.
Here filter F1 is mapped to servlets S1, S2, and S3, filter F2 is mapped to servlet S2, and filter F3 is mapped to servlets S1 and S2.
You apply filters via configuration in your web.xml file by URL patterns or servlet names.
See section 6.2.4 in the servlet spec for the rules used in determining the chaining order.
Here is an excerpt:
The order the container uses in building the chain of filters to be applied for a
particular request URI is as follows:
1. First, the <url-pattern> matching filter mappings in the same order that these
elements appear in the deployment descriptor.
2. Next, the <servlet-name> matching filter mappings in the same order that
these elements appear in the deployment descriptor.
If a filter mapping contains both <servlet-name> and <url-pattern>, the
container must expand the filter mapping into multiple filter mappings (one for
each <servlet-name> and <url-pattern>), preserving the order of the <servletname>
and <url-pattern> elements. For example, the following filter mapping: