2. WHAT IS FILTERS?
• Java class that is called for responding to requests for
resources(Servlet / JSP)
• Dynamically change the behaviour of the resource
• Intercept and process the requests
• Process the responses after responses has been generated by
servlet
• Encapsulates and modifies values of request, response or header
• Introduced in Servlet 2.3 specification
3. EXPLORING THE NEED OF
FILTERS
• Suppose application is in the format of Excel.
• At later point of time, clients may require it in another format
like PDF, HTML etc.
• Instead of modifying code every time, filter can be created to
transform data dynamically in required formats.
4. FEATURE AND UTILITY
• Main feature : Reusabilty
• Situations where filters are used:
1. Security verification
2. Logging operations
3. Session validation
4. Encryption
5. EXPLORING THE WORKING OF
FILTERS
1. When a request is sent for specific resource, request is
intercepted by a filter.
2. To intercept a request, a filter should have access to HTTP
request and response objects.
3. Objects are accessed by javax.servlet.ServletRequest and
javax.servlet.ServletResponse objects
4. Needs to access the list of chained filters, which can accessed
using javax.servlet.FilterChain object.
6. EXPLORING FILTER API
• The Filter API includes three interfaces:
1. Filter
2. FilterConfig
3. FilterChain
7. FILTER INTERFACE
• A Filter can be created in an application by implementing the
javax.servlet.Filter interface.
• Filter interface calls the following methods during life cycle of a
filter:
1. Init() method
2. doFilter() method
3. Destroy() method
8. THE Init() METHOD
• Init() method is invoked by the web container only once when
the filter is initialized.
• Servlet container passes the FilterConfig object as a parameter
through the init() method.
Syntax:
public void init(FilterConfig filterconfig) throws
ServletException
9. THE doFilter() METHOD
• doFilter() method is invoked when a user requests a resource,
such as a servlet to which the filter is mapped.
• Syntax:public void doFilter(ServletRequest
request,ServletResponse response,FilterChain chain) throws
java.IOException,Servlet Exception.
10. THE doFilter() METHOD
doFilter() method can be implemented to perform the following
tasks:
• Examining the request.
• Carrying out input filtering by wrapping the request object with
filter content or headers.
• Carrying out output filtering by wrapping the response object
with filter content or headers.
• Invoking the next filter in filter chain by calling the FilterChain
object or blocking the request by not forwarding the
request/response pair to next filter.
11. destroy() METHOD
• destroy() method is invoked when the filter instance is
destroyed.
• Once the destroy() method is called by the web container,the
doFilter() method cannot be called again on this filter instance.
• destroy() method enables to release any resources held by the
filter.
13. FILTERCONFIG INTERFACE
• The FilterConfig interface used is stored the initialized data.
• The init() method of the Filter interface takes a filter
configuration object as an argument, which is an instance of
FilterConfig interface.
• The filter recieves filter configuration information during
initialization from a servlet container through the FilterConfig
object.
14. METHODS OF FILTERCONFIG
INTERFACE
1. getFilterName:
• Returns the filter name as assigned in Deployment Descriptor.
Syntax:
public java.lang.String getFilterName()
2. getInitParameter:
• Returns the named initialization parameter value as a string.
• A null value is returned if the parameter is not found.
• Syntax:
Public java.lang.String getInitParameter(java.lang.String name)
15. METHODS OF FILTERCONFIG
INTERFACE
3. getInitParameterNames:
• Returns an Enumeration of String objects.
• An empty Enumeration is returned,if no initialization parameters
are found for the filter.
Syntax:
public java.util.Enumeration getInitParameterNames().
4. getServletContext:
• Returns a reference of the ServletContext interface in which a
filter is executed.
Syntax:
publicServletContext getServletContext()
16. FILTERCHAIN INTERFACE
• FilterChain interface provides a mechanism to invoke a series of
filters,which are specified in filter chain,in an application.
• If the invoked filter is the last one in chain,the target resource
is invoked.
• Syntax:
• Public void doFilter(ServletRequest request,ServletResponse
response) throws java.io.IOException,ServletException.
17. CONFIGURING A FILTER
• web.xml is used to configure filters to resources
• Annotations- Servlet 3.0
• Deployment descriptor used for defining configuration and
mapping for filters and servlets
• Use of annotations eliminates the need of deployment
descriptor
18. CONFIGURING FILTERS USING
DEPLOYMENT DESCRIPTOR
• Deployment descriptor is located in WEB-INF directory of the
web application.
• Used to declare and map the filter either to specific or multiple
URL patterns
• <filter> and <filter-mapping> elements are used for configuring
filters