Servlet Filters: A.D. P I T Advanced Java (: A.Y. 2017-18
Servlet Filters: A.D. P I T Advanced Java (: A.Y. 2017-18
Servlet Filters: A.D. P I T Advanced Java (: A.Y. 2017-18
SERVLET FILTERS
P REPARED BY:
KUNAL PARIKH - 160010116023
HARSHIL PATEL - 160010116038
LAJJA PATEL - 160010116041
B.E (IT) – SEM 6
GUIDED BY:
PROF NAYAN MALI
(ADIT)
JSP - FILTERS
Servlet and JSP Filters are Java classes that can be used in Servlet
and JSP Programming for the following purposes:
• Authentication Filters.
• Data compression Filters
• Encryption Filters .
• Filters that trigger resource access events.
• Image Conversion Filters .
• Logging and Auditing Filters.
• MIME-TYPE Chain Filters.
• Tokenizing Filters .
• XSL/T Filters That Transform XML Content.
ONE OR MANY FILTERS
Filters can perform many different types of functions :
When the web container starts up your web application, it creates an instance of
each filter that you have declared in the deployment descriptor. The filters
execute in the order that they are declared in the deployment descriptor.
SERVLET FILTER METHODS:
S.N
Method & Description
.
1 public void doFilter (ServletRequest, ServletResponse, FilterChain)
This method is called by the container each time a request/response pair is passed through the
chain due to a client request for a resource at the end of the chain. Performs the actual filtering
work
Examines response headers after it has invoked the next filter in the
chain
Compile LogFilter.java in usual way and put your LogFilter.class class file in
<Tomcat-installation-directory>/webapps/ROOT/WEB-INF/classes.
JSP FILTER MAPPING IN WEB.XML:
Filters are defined and then mapped to a URL or JSP file name, in much
the same way as Servlet is defined and then mapped to a URL pattern in
web.xml file. Create the following entry for filter tag in the deployment
descriptor file web.xml
<web-app>
…..
<filter>
<filter-name>LogFilter</filter-name>
<filter-class>LogFilter</filter-class>
<init-param>
<param-name>test-param</param-name>
<param-value>Initialization Paramter</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>LogFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
……
</web-app>
The above filter would apply to all the servlets and JSP because we
specified /* in our configuration. You can specify a particular servlet or
JSP path if you want to apply filter on few servlets or JSP’s only.
Now try to call any servlet or JSP in usual way and you would see
generated log in your web server log. You can use Log4J logger to log
above log in a separate file.
USING MULTIPLE FILTERS:
Your web application may define several different filters with a specific purpose.
<filter>
<filter-name>LogFilter</filter-name>
<filter-class>LogFilter</filter-class>
<init-param>
<param-name>test-param</param-name>
<param-value>Initialization Parameter</param-value>
</init-param>
</filter>
<filter>
<filter-name>AuthenFilter</filter-name>
<filter-class>AuthenFilter</filter-class>
<init-param>
<param-name>test-param</param-name>
<param-value>Initialization Parameter</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>LogFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>AuthenFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
FILTERS APPLICATION ORDER:
The order of filter-mapping elements in web.xml determines the order in which the web
container applies the filter to the servlet. To reverse the order of the filter, you just need to
reverse the filter-mapping elements in the web.xml file.
For example, above example would apply LogFilter first and then it would apply
AuthenFilter to any servlet but the following example would reverse the order:
<filter-mapping>
<filter-name>AuthenFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>LogFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>