This document discusses servlet filters in Java web applications. It explains that filters can perform preprocessing and postprocessing tasks on requests and responses, such as validation, compression, encryption, and logging. Filters are pluggable and their configuration is defined in the web application's XML file. The key methods for implementing a filter are init(), doFilter(), and destroy(). doFilter() processes the request and response, and can pass them to the next filter in the chain before they reach the servlet. Filters provide a way to customize and enhance the request-response lifecycle and add common features to multiple servlets.
1. NADAR SARASWATHI COLLEGE OF
ARTS AND SCIENCE
VADAPUTHUPATTI, THENI – 625 531
Department of Computer Science and Information Technology
PRESENTED BY
NIBIYA.G
I-MSC(INFORMATION TECHONOLGY)
3. SERVLET FILTERS
A filter is an object that is invoked at the pre-processing &
post-processing of a request
It is mainly used to perform filtering tasks such as conversion,
logging, compression , encryption and decryption, input
validation etc.
The servlet filter is pluggable
i.e. its entry is defined in the web
4. SERVLET FILTERS
Discuss about customization of request and response using filters
Filters are used to process the content in the request or
response object of servlet and generally they do not
produce the response.
5. Filter interrupt
Requests before the servlet was processed
Response immediately after the was processed or before the response to the client.
Enhance the lifecycle process
Used to perform various function the multiple servlet in a
web application
To add features to existing servlet code
Also create reusable function to use in multiple web
application
6. EXAMPLE:-
Customize the authentication process using filter
Filter intercept the request check whether the sufficient permission was
given to the user to access the request servlet
Prevent the unauthorized user from accessing the
servlet
7. Used to record
User action
Compress the output streams
Very flexible because we won’t write the filters with in
servlet
Configured in the web. Xml
9. To write a filter, we have to implement the filter interface
“filter” interface.
Contains three methods:
Int()
dofilters()
Destroy()
10. INT() method
Called by the web contains to indicate the filter that, it is
available for service
Can’t use the filter before this method was executed
Structure of the init method
public void init(FilterConfig filterConfig)throws
javax.servlet.ServletException
{
// write your codes
}
Used to provide any
configuration parameters
11. doFilter() methods
Heart of the filter
Write the processors for filtering
Method has three parameters
request
Response
Object of filterchain
Also called by the web container
Does all the filter processer
12. Calls filterChain. doFilter() to check any other filters are
available to process this request
If any, it will pass the request and response object to that filter
Otherwise it will do post processing filtering the response
signature of the method:
13. Public void dofilter(ServletRequest request, ServletResponse
response, FilterChain chain)throws IoException,
ServletException
{
//write the code to process the request & response
}
14. Destroy() methods
Used to remove the filter from the service
General form
Public void destroy()
{
//write your codes
}
15. Filtering chaining
Client filter-1 filter-2 filter-3 servlet
Also use more than one filter for customizing a particular
servlet.
Request
Response
17. dofilter (req, res, chain)
{
chain. dofilter(req.res)
}
code to filter the
request
code to filter the
response
To process the request or to
filter the request
To intercept the servlet output
or to filter the response