FILTERS
Filter is an object that performs the filtering tasks on
either the request to a resource or response to resource.
Examples that
have been identified for this design are
1) Authentication Filters
2) Logging and Auditing Filters
3) Image conversion Filters
4) Data compression Filters
5) Encryption Filters
6) Tokenizing Filters
7) Filters that trigger resource access events
8) XSL/T filters
9) Mime-type chain Filter
1) Authentication Filters
2) Logging and Auditing Filters
3) Image conversion Filters
4) Data compression Filters
5) Encryption Filters
6) Tokenizing Filters
7) Filters that trigger resource access events
8) XSL/T filters
9) Mime-type chain Filter
Filter is a java program which
provide the implementation of filter interface. This interface consist of 3
methods
1. init(FilterConfig
config)
2.
doFilter(ServletRequest
request, ServletResponse response, FilterChain chain)
3. destroy()
When we send request to the filter,
server create the filter object and it calls the init method, then server
execute the doFilter() . Form second time onwards server execute the
doFilter(). If filter is removed from the server, server call the destroy().
When we configure the filter in the
web.xml file it is recommended to use url like "*"
Project Structure:
MyFirstFilter:
public class MyFirstFilter implements Filter
{
FilterConfig config;
public void init(FilterConfig config)
{
this.config = config;
System.out.println("init");
}
public void doFilter(ServletRequest request,
ServletResponse response, FilterChain chain)
{
System.out.println("My First
Filter");
}
public void destroy()
{
System.out.println("Destroy");
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<filter>
<filter-name>firstFilter</filter-name>
<filter-class>com.ibm.filter.MyFirstFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>firstFilter</filter-name>
<url-pattern>/ff</url-pattern>
</filter-mapping>
</web-app>
URL:
http://www.localhost:8089/Filters/ff
-----------------------------------------------------------------------------------------------------------------------
·
In doFilter() if we call chain.dofilter(request,
response) first server checks are there
any filters are available to execute, then server execute the filter if same
code available here server checks for other filters, nothing is present server
call the resource
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<filter>
<filter-name>firstFilter</filter-name>
<filter-class>com.ibm.filter.MyFirstFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>firstFilter</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
<filter>
<filter-name>mySecondFilter</filter-name>
<filter-class>com.ibm.filter.MySecondFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>mySecondFilter</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>welcome</servlet-name>
<jsp-file>/Welcome.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>welcome</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
</web-app>
package com.ibm.filter;
public class MyFirstFilter implements Filter
{
FilterConfig
config;
public
void init(FilterConfig config)
{
this.config
= config;
System.out.println("init");
}
public
void doFilter(ServletRequest request, ServletResponse response, FilterChain
chain) throws IOException, ServletException
{
System.out.println("My
First Filter");
chain.doFilter(request,
response);
}
public
void destroy()
{
System.out.println("Destroy");
}
}
public class MySecondFilter implements Filter
{
public void destroy()
{
System.out.println("Second Filter
destroy");
}
public void doFilter(ServletRequest request,
ServletResponse respone,
FilterChain chain) throws IOException, ServletException
{
System.out.println("Second Filter
doFilter");
}
public void init(FilterConfig config) throws ServletException
{
System.out.println("second filter
init");
}
}
Logging Filter:
public class MyFirstFilter implements Filter
{
FilterConfig config;
public void init(FilterConfig config)
{
this.config = config;
System.out.println("init");
}
public void doFilter(ServletRequest request,
ServletResponse response, FilterChain chain) throws IOException, ServletException
{
HttpServletRequest requ =
(HttpServletRequest)request;
ServletContext application = requ.getServletContext();
application.log("" + requ.getRemotePort());
Enumeration<String> enu =
requ.getHeaderNames();
while (enu.hasMoreElements())
{
String s =
enu.nextElement();
application.log(requ.getHeader(s));
}
chain.doFilter(request, response);
}
public void destroy()
{
System.out.println("Destroy");
}
}
In the above filter we will use logging
statements for finding the user details.
No comments:
Post a Comment