1. LISTENERS AND FILTERS
Using ServletContextListener,
HttpSessionListener,
Understanding of all the other Listeners viz.
ServletRequestListener,
ServletContextAttributeListener,
ServletRequestAttributeListener,
HttpSessionAttributeListener
2. LISTENERS AND FILTERS
Listeners are the classes which listens to a particular type
of events and when that event occurs , triggers the
functionality.
Each type of listener is bind to a type of event.
There are two most widely used Servlet Listener i.e.
ServletContextListener and HttpSessionListener.
With listeners you can track application-level, session-level,
life-cycle changes, attribute changes etc.
The implemented interfaces are javax.servlet.Listener
interface.
Servlet Filter is used for monitoring request and response
from client to the servlet, or to modify the request and
response, or to audit and log.
3.
4.
5. LISTENERS
There are total eight type of listeners available in servlet
framework which listens to a particular event and they are
ServletContextListener
ServletContextAttributeListener
HttpSessionListener
HttpSessionAttributeListener
ServletRequestListener
ServletRequestAttributeListener
HttpSessionActivationListener
HttpSessionBindingListener
As the configurations of servlets , filters goes inside web.xml ,
similarly listeners are also configured inside web.xml using
<listener> </listener> tag.
6. SERVLETCONTEXTLISTENER
ServletContextListener will be executed once
your web application is deployed in your
application server (Tomcat or etc).
If you have any requirements that need to be
executed before the application is started,
ServletContextListener is the best place for you.
ServletContextListener listens to
SessionContextEvent event which gives a
notification when Servlet Context is initialized or
destroyed and used on the events
ServletContextListener executes the functionality.
7. ServletContextListener is the interface and it defines two
methods –
void contextDestroyed(ServletContextEvent e) – This
method is executed when application is destroyed
void contextInitialized(ServletContextEvent e)- This
method is executed when application is initialized
8. ServletContext object can be obtained
from ServletContextEvent and listener can set the
attributes in Servlet context object which can be used in
servlets later.
We can use the “ServletContextListener “ listener for any
activity that is required either at the application deployment
time or any clean up activity required when application is
destroyed.
One of the practical example that is initializing database
connections and clean up of database connections.
9.
10.
11. HTTPSESSIONLISTENER
HttpSessionListener listens to HttpSessionEvent event
which gives a notification when session is created or
destroyed.
The HttpSessionEvent is notified when session object is
changed. The corresponding Listener interface for this
event is HttpSessionListener.
We can perform some operations at this event such as
counting total and current logged-in users, maintaing a log
of user details such as login time, logout time etc.
12. METHODS OF HTTPSESSIONLISTENER INTERFACE
There are two methods declared in the HttpSessionListener
interface which must be implemented by the servlet
programmer to perform some action.
public void sessionCreated(HttpSessionEvent e): is
invoked when session object is created.
public void sessionDestroyed(ServletContextEvent e):
is invoked when session is invalidated.
13. SERVLETCONTEXTATTRIBUTELISTENER
ServletContextAttributeListener listens to
ServletContexAttributetEvent event which gives a notification
when any object is added, removed or replaced from servlet
context .
ServletContextAttributeListener is the interface and it defines
three methods –
attributeAdded(ServletContextAttributeEvent e): It notifies
whenever a new attribute is added to the servlet context.
attributeRemoved(ServletContextAttributeEvent e): It
notifies whenever the attribute is removed from the servlet
context.
attributeReplaced(ServletContextAttributeEvent e): It
notifies whenever the attribute gets replaced on the servlet
context.
14. Attribute name and value that has been added, removed or
replaced can be obtained from the getName() and
getValue() method of ServletContextAttributeEvent
15. HTTPSESSIONATTRIBUTELISTENER
HttpSessionAttributeListener listens to
HttpSessionBindingEvent event which gives a notification when
any object is added, removed or replaced from session .
HttpSessionAttributeListener is the interface and it defines three
methods –
attributeAdded(HttpSessionBindingEvent e): It notifies
whenever a new attribute is added to the session.
attributeRemoved(HttpSessionBindingEvent e): It notifies
whenever the attribute is removed from the session.
attributeReplaced(HttpSessionBindingEvent e): It notifies
whenever the attribute gets replaced on the session.
Attribute name and value that has been added, removed or
replaced can be obtained from the getName() and getValue()
method of HttpSessionBindingEvent
16. SERVLETREQUESTLISTENER
ServletRequestListener listens to ServletRequestEvent
event which gives a notification when request is created or
destroyed.
ServletRequestListener is the interface and it defines two
methods –
void requestDestroyed(ServletRequestEvent e) – This
method is executed when request is destroyed
void requestInitialized(ServletRequestEvent e)- This
method is executed when request is initialized.
Request object can be Obtained from HttpRequestEvent.
17. SERVLETREQUESTATTRIBUTELISTENER
ServletRequestAttributeListener listens to
ServletRequestAttributeEvent event which gives a notification when
any object is added, removed or replaced from request .
ServletRequestAttributeListener is the interface and it defines three
methods –
attributeAdded(ServletRequestAttributeEvent e): It notifies whenever
a new attribute is added to the request.
attributeRemoved(ServletRequestAttributeEvent e): It notifies
whenever the attribute is removed from the request.
attributeReplaced(ServletRequestAttributeEvent e): It notifies
whenever the attribute gets replaced on the request.
Attribute name and value that has been added, removed or
replaced can be obtained from the getName() and getValue() method of
ServletRequestAttributeEvent
18. INTRODUCTION TO FILTER API
Filters are compontents that you can use and
configure to perform some filtering tasks.
Filter is used for pre-processing of requests and
post-processing of responses.
You can have any number of filters for pre-
processing of a request and post-processing of a
response.
Filters are configured in the deployment descriptor
of a web application.
19.
20. HOW FILTERS WORKS?
When a request reaches the Web Container, it checks if any
filter has URL patterns that matches the requested URL.
The Web Container locates the first filter with a matching URL
pattern and filter's code is executed.
If another filter has a matching URL pattern, its code is then
executed. This continues until there are no filters with
matching URL patterns left.
If no error occurs, the request passes to the target servlet.
Hence we know, that the request will be passed to the target
servlet only when all the related Filters are successfully
executed.
The servlet returns the response back to its caller. The last filter that was
applied to the request is the first filter applied to the response.
At last the response will be passed to the Web Container which passes it
to the client.
21. Filter API is part of Servlet API.
Filter interface is found in the javax.servlet package.
For creating a filter, we must implement Filter interface.
Filter interface gives the following life cycle methods for a
filter:
void init(FilterConfig filterConfig): invoked by the web
container to indicate to a filter that it is being placed into
service.
void doFilter(ServletRequest request, ServletResponse
response, FilterChain chain): invoked 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.
void destroy(): invoked by the web container to indicate to
a filter that it is being taken out of service.
22. WHAT IS FILTERCHAIN INTERFACE?
FilterChain object is used to invoke the next filter in the
chain, or if the calling filter is the last filter in the chain
then the rosource at the end of the chain invoked.
The resources at the end of Filter chain can either be a
target Servlet(in case of request flow) or the Client(in case
of response flow) as described in the diagram.
24. EXAMPLE DEMONSTRATING FILTER USAGE
In this example we are using Filter to authenticate(check
correct username and password).
Here index.html will ask username and password from
the user, MyFilter will validate the password entered by
the user, if the user has entered "1234" as password, then
he will be forwarded to first servlet else the index.html will
be shown again to the user.