Slide 12 EventListener
Slide 12 EventListener
Objectives
Servlet listener
Session listener
ServletContext listener
03/28/24 2/41
Java web server Event
• Web events are key moments in web application activity such as
initialization of application, destroying an application, request from
client, creating/destroying a session, attribute modification in session
etc.
• Event listener is a component that waits for an event to occur and
react to that event by calling the event listener method.
• Servlet API provides classes and interfaces to handle lifecycle events
of important objects: servlet, session, servlet context
• Each event type will have a corresponding listener class defined
by implementing listener interface
• For example, we can create a Listener for the application startup
event to read context init parameters and create a database
connection and set it to context attribute for use by other resources.
03/28/24 3/41
Web events and listeners
Event & listener types on Java web application:
ServletRequestEvent ServletRequestListener
ServletContextEvent ServletContextListener
ServletRequestAttributeEvent
ServletRequestAttributeListener
ServletContextAttributeEvent ServletContextAttributeListener
HttpSessionEvent HttpSessionListener
HttpSessionBindingEvent HttpSessionBindingListener
HttpSessionAttributeListener
03/28/24 4/41
Listener programming
In Netbean, create new web application listener, then
select listener type, override the interested method
03/28/24 5/41
ServletRequest events-listeners
ServletRequestEvent –indicate lifecycle
events for a servlet request
ServletRequestListener
void requestDestroyed(ServletRequestEvent sre)
Receives notification that a ServletRequest is about to go out of
scope of the web application.
void requestInitialized(ServletRequestEvent sre)
Receives notification that a ServletRequest is about to come
into scope of the web application
When a servlet request listener is installed, the user request not
only calls the requested Servlet but also calls the listener class
03/28/24 6/41
ServletRequestAttributeListener
ServletRequestAttributeListener can listen to these
events:
When an attribute is added to an ServletRequest object by invoke
request.setAttribute(name, obj)
void attributeAdded(ServletRequestAttributeEvent srae)
When an attribute of an ServletRequest object is replaced by
invoke request.setAttribute(name, obj) with new obj.
void attributeReplaced(ServletRequestAttributeEvent srae)
When an attribute of an ServletRequest object is deleted by
invoke request.removeAttribute(name)
void attributeRemoved(ServletRequestAttributeEvent srae)
ServletRequestAttributeEvent:
store the name and value information of the attribute
03/28/24 7/41
HttpSessionListener
HttpSessionListener can listen to these events
When an HttpSession is created in a web application
by container or invoke request.getSession(true)
void sessionCreated(HttpSessionEvent hse)
When an HttpSession is invalidated in a web
application when session timeout or invoke
session.invalidate()
void sessionDestroyed (HttpSessionEvent hse)
We can perform some operations with this listener such as
counting total and current logged-in users, maintain a log of user
login time, logout time etc
03/28/24 8/41
HttpSessionAttributeListener
HttpSessionAttributeListener –receiving notification
events about HttpSession attribute changes:
void attributeAdded(HttpSessionBindingEvent event)
void attributeReplaced(HttpSessionBindingEvent event)
void attributeRemoved(HttpSessionBindingEvent event)
HttpSessionBindingListener is a callback interface that can be
implemented by classes which are intended to be added to
session. Whenever an object implementing
HttpSessionBindingListener is added or removed to session, the
callback methods defined on the object is called
The key difference between HttpSessionAttributeListener and
HttpSessionBindingListener is that the first listens for any
attribute change in session while the second gets notified whenever
the same object is added/removed from session
03/28/24 9/41
ServletContextListener
ServletContextListener execute code on webapp startup
and shutdown:
void contextInitialized(ServletContextEvent sce)
Is triggered when the web application is starting the initialization.
This will be invoked before any of the filters and servlets are
initialized
void contextDestroyed(ServletContextEvent sce)
Is triggered when the ServletContext is about to be destroyed.
This will be invoked after all the servlets and filters have been
destroyed
ServletContextListener may be configured using annotations
@WebListener or in web.xml
03/28/24 10/41
ServletContextAttributeListener
ServletContextAttributeListener is listen for context attribute change events
The ServletContextAttributeListener has following methods:
• void attributeAdded(ServletContextAttributeEvent scae)
The servlet container calls this method to notify the listener that an attribute
was added to the servlet context.
03/28/24 11/41
Event Listener Declaration
Event listeners are declared in the application
web.xml deployment descriptor through <listener>
elements under the top-level <web-app> element.
Each listener has its own <listener> element, with
a <listener-class> sub-element specifying the class
name.
Within each event category, event listeners should be
specified in the order in which you would like them to be
invoked when the application runs.
03/28/24 12/41
Event Listener in web.xml
Here is an example of event listener declarations in web.xml:
<web-app>
<listener>
<description>ServletContextListener</description>
<listener-class>controller.AppListener</listener-class>
</listener>
<listener>
<description>HttpSessionListener</description>
<listener-class>controller.SessionListener</listener-class>
</listener>
</web-app>
03/28/24 13/41
Coding and Deployment Guidelines
Be aware of the following rules and guidelines for event listener
classes:
•Each listener class file must be packaged in the application WAR file,
either under /WEB-INF/classes or in a JAR file in /WEB-INF/lib.
03/28/24 14/41
Summary
1. What are Web Event and Listeners?
03/28/24 15/41
Constructivity question
Init parameters of a web application declared in web.xml are used
to store application-wide shared data, but it can only accept
primitive data type and String. Give a solution to share data of any
reference type
When an item is selected by a customer to be added to the
shopping cart, it needs to be checked for inventory to promptly
notify the customer whether the item is available or not. How
should this be done?
Is it possible to install multiple listeners of the same type for a web
application? What is going to happen?
Where should the hit counter of a web application be installed?
Provide a solution so that when the web application is shutdown
and restarted, the counter will continue to count.
03/28/24 16/41