In this session, you will learn to:
Use Servlet Event Listeners
Objectives
iNET Java/JSP/Servlet Course
Servlet Event Listeners
A servlet event listener is a Java program that listens for events associated with a servlet.
A listener can be used to monitor and react to events on a servlet life cycle by defining listener objects.
The methods of these listener objects are invoked when life cycle events occur.
To use these listener objects, you must define and specify the listener class.
The servlet container generates events that cause the event listener classes to do something.
iNET Java/JSP/Servlet Course
Application event listeners are classes that implements one or more of the servlet event listener interfaces.
You can have multiple listener classes listening to each event type and specify the order in which the container invokes the listener beans for each event type.
The various listener interfaces are:
ServletContextAttributeListener HttpSessionListener
ServletRequestListener
ServletRequestAttributeListener HttpSessionBindingListener
HttpSessionAttributeListener ServletContextListener
HttpSessionActivationListener
Types of Event Listeners
iNET Java/JSP/Servlet Course
You need to perform the following tasks to track the number of concurrent users:
Create a listener class.
Put a <listener> element in the web.xml deployment descriptor.
Create a servlet.
Deploy the servlet
Creating a Event Listener
iNET Java/JSP/Servlet Course
Challenge
Which of the following listener interfaces will you use if you want to know if an attribute in a web app context has been added, removed, or replaced?
a. ServletContextAttributeListener b. ServletRequestAttributeListener c. HttpSessionListener
d. HttpSessionBindingListener
Answer:
a. ServletContextAttributeListener
iNET Java/JSP/Servlet Course
Challenge (Contd.)
Which of the following listener interfaces will you use if you want to know about the number of concurrent users?
a. ServletRequestAttributeListener b. HttpSessionListener
c. ServletRequestListener
d. HttpSessionBindingListener
Answer:
b. HttpSessionListener
iNET Java/JSP/Servlet Course
Challenge (Contd.)
Which of the following listener interfaces will you use if you want to know each time a request comes in, so that you can log it?
a. ServletRequestAttributeListener b. HttpSessionBindingListener
c. ServletRequestListener d. HttpSessionListener
Answer:
c. ServletRequestListener
iNET Java/JSP/Servlet Course
Challenge (Contd.)
Which of the following listener interfaces will you use if you want to know when a request attribute has been added, removed, or replaced ?
a. ServletRequestAttributeListener b. HttpSessionBindingListener
c. HttpSessionListener d. ServletRequestListener
Answer:
c. ServletRequestAttributeListener
iNET Java/JSP/Servlet Course
Challenge (Contd.)
Which of the following listener interfaces will you use if you want to know when a session attribute has been added, removed, or replaced?
a. ServletRequestAttributeListener b. HttpSessionBindingListener
c. HttpSessionAttributeListener d. ServletRequestListener
Answer:
c. HttpSessionAttributeListener
iNET Java/JSP/Servlet Course
In this session, you learned to:
Use Servlet Event Listeners
Summary