How to Link Servlet Filter with our Servlet web resource programs:
1) To link One filter program with One servlet program:
URL-pattern of Srv1 (Servlet program) : /s1url
URL-pattern of Frv1 (Filter program) : /s1url
2) To link Two filter's program with One servlet program:
URL-pattern of Srv1 (Servlet program) : /s1url
URL-pattern of Frv1, Frv2 (Filter programs) is : /s1url
Note : when multiple filters are linked with one servlet program these filters will be executed in the order they are configured in web.xml while trapping request's and same filters will be executed in reverse order while trapping the response.
3) To link single Filter program with all web resource programs of web application:
Srv1 program url pattern is : /s1url
Srv2 program url pattern is : /s2url
Srv3 program url pattern is : /s3url
Url-pattern of Frv1 program(Filter Program): /*
4) To link certain filters with certain program:
Srv1, Srv2, Srv3 & Srv4 are servlet programs.
Frv1, Frv2 are servlet Filter programs.
Srv1 url pattern : /x/s1url
Srv2 url pattern : /x/s2url
Srv3 url pattern : /y/s3url
Srv4 url pattern : /y/s4url
Frv1 Filter url-pattern : /x/* (Frv1 is linked with Srv1, Srv2 programs)
Frv2 Filter url-pattern : /y/* (Frv2 is linked with Srv3, Srv4 programs)
5) To link filter with JSP program:
JSP program url-pattern : /test1
Frv1 program url-pattern : /test1
Note:
When Filter is mapped with Servlet then the Filter and servlet program will use same request and response objects while processing that request.
Real time use of filetrs:
--------------------------
To count number of request coming to web application we need to use filter that is mapped with all web-resource programs.
When end users try to access important resource to check he/she is their in session or not we need servlet filter.