This document provides an overview of filters in Java servlets. Filters intercept requests and responses between clients and servlets/JSPs. Filters are configured in web.xml and implement init(), doFilter(), and destroy() methods. Filters can manipulate requests and responses using wrappers and operate in a filter chain. Servlets can communicate by forwarding requests or including output from one servlet in another.
4. Slide 4 of 25
Filters
Components that add functionality to the
request and response processing of a
Web Application
Intercept the requests and responses that
flow between a client and a Servlet/JSP.
5. Slide 5 of 25
Filter Participation
User Filter
Web Resource
(Servlet / JSP)
Request
Response
Filter Participating in Request-Response Process
6. Slide 6 of 25
Filter Life Cycle
Instantiation
and Loading
Initialization
init()
doFilter()
destroy()
Unavailable
GC
7. Slide 7 of 25
Filter Chain
Invoke a series of filters
If the Calling filter is last filter, will invoke web resource
Filter
1
Client
Filter
2
Filter
3
Web
Resourc
e
Working of Filter Chain
Filter Chain
8. Slide 8 of 25
Usage of Filter
Authentication
Logging and auditing filter
Image conversion filters
Data compression
Encryption filters
Filters that trigger resource access events
14. Slide 14 of 25
“Filter” Interface
A filter is an object than perform filtering tasks
on either the request to a resource (a
servlet or static content), or on the response
from a resource, or both.
Init()
doFilter()
Destroy()
15. Slide 15 of 25
“FilterConfig” Interface
A filter configuration object used by a servlet
container used to pass information to a filter
during initialization
Methods
– getFilterName()
– getInitParameter()
– getInitParameterNames()
– getServletContext()
16. Slide 16 of 25
“FilterChain” Interface
A FilterChain is an object provided by the
servlet container to the developer giving a
view into the invocation chain of a filtered
request for a resource
Methods
– doFilter()
21. Slide 21 of 25
Manipulating Request and Response
22. Slide 22 of 25
Manipulating Request and Response
ServletRequestWrapper
ServletResponseWrapper
23. Slide 23 of 25
Inter-Servlet Communication
A process where two or more Servlets
communicates with each other to process
the client request.
A Servlet can forward the request to
another Servlet to process the client
request.
A Servlet can include the output of another
Servlet to process the client request.
24. Slide 24 of 25
Applet – Servlet Communication
Developing Applet front end application
Establishing connection between applet and
servlet
Communicasting throught object
serialisation
Sending objects from applet to servlet
Sending objects from servlet to applet
25. Slide 25 of 25
Summary
Introduction to Filters
Filter API
Configuring Filter
Manipulating Request and Responses
Servlet Communication