Nearby lessons
33 of 34Servlet - Examples: Listeners
- See complete runnable servlet programs
- Understand the output of each program
- Copy and deploy programs in Tomcat
Listener programs for Request, Context, Session, Binding, and Activation listeners.
ServletRequestEvent(C)
ServletRequestEvent is the child class of java.util.EventObject.
EventObject class contains one method getSource()
public Object getSource()
It returns the source which causes the event. In this case web application is the source of event
& hence we will get ServletContext object.
FirstSevlet.java
RequestDemoListener.java
web.xml:
RequestDemoListener.java
|-WEB-INF
|-web.xml
|-classes
|-FirstServlet.class
|-RequestDemoListener.class
Note:
- We can configure Listener in web.xml by using <listener> tag. <listener> tag is the direct child
tag of <web-app> and hence we can take anywhere within <web-app>
- We can configure more than one listener of same type. The order of execution of these listeners
is depends on the order of <listener> tags in web.xml
- Web container is responsible for the creation of Listener class object.For this web container
always calls public no-arg constructor. Hence every listener class should compulsary contains
public no-arg constructor.
- Web container will create Listener object automatically at the time of either server startup or at
application deployment.
RequestDemoListener.java
This listener listens the events related to request scoped attributes like attribute
addition,attribute removed and attribute replaced.
This interface defines the following 3 methods
RequestDemoListener.java
This method will be executed automatically by the web container whenever we are adding an
attribute in the request scope.
RequestDemoListener.java
ServletRequestAttributeEvent(C)
Returns the name of the attribute which is added or removed or replaced in the request scope.
ServletRequestAttributeEvent(C)
returns the value of the attribute which is added or replaced or removed.
In the case of attribute addition and removal this method returns the corresponding attribute
value.But in the case of replacement this method returns old value of the attribute.
FirstSevlet.java
RequestAttributeDemoListener.java
web.xml:
RequestAttributeDemoListener.java
|-WEB-INF
|-web.xml
|-classes
|-FirstServlet.class
|-RequestAttributeDemoListener.class
ServletContextListener
This method will be executed automatically by the web container at the time of context object
creation. i.e at the time of application deployment or server startup.
ServletContextListener
This method will be executed automatically by the web container at the time of context object
destruction. i.e at the time of application undeployment or server shutdown.
ServletContextEvent(C)
Increments count value for every request.
ServletContextEvent(C)
ServletContextEvent(C)
saves the count value to abc.txt file at the time of context object destruction(server shutdown)
It reads the count value from abc.txt file and assign to RequestDemoListener.count variable at
context object creation(server startup)
ServletContextEvent(C)
3.FirstServlet.java:
To display count value to end user
ServletContextEvent(C)
4.web.xml:
For declaring listeners
ServletContextEvent(C)
ServletContextEvent(C)
To save count value
listenersc
|-abc.txt
|-WEB-INF
|-web.xml
|-classes
|-FirstServlet.class
|-RequestDemoListener.class
|-ContextDemoListener.class
ServletContextAttributeListener(I)
ServletContextAttributeEvent(C)
HttpSessionListener(I)
Demo Program to display the number of users currently online
This listener increments count value for every session object creation and decrements count value
for every session object destruction.
Demo Program to display the number of users currently online
Demo Program to display the number of users currently online
Demo Program to display the number of users currently online
Demo Program to display the number of users currently online
Demo Program to display the number of users currently online
|-WEB-INF
|-web.xml
|-classes
|-FirstServlet.class
|-SessionCounter.class
Demo Program to display the number of users currently online
This listener listens the events related to session scoped attributes like attribute addition,removal
and replacement.
This interface defines the following 3 methods.
public void attributeAdded(HttpSessionBindingEvent e)
public void attributeRemoved(HSBE e)
public void attributeReplaced(HSBE e)
Note:
There is no event class named with HttpSessionAttributeEvent. For this equivalent event class is
HttpSessionBindingEvent.
HttpSessionBindingListener(I)
This method will be executed automatically at the time of attribute addition.
HttpSessionBindingListener(I)
This method will be executed automatically at the time of attribute removal.
Note: In the case of replacement both methods will be executed but valueBound() method first
followed by valueUnbound().
Note:
It is not required to configure HttpSessionBindingListener in web.xml. Whenever we are adding an
attribute,web container will check the corresponding class implements HttpSessionBindingListener
or not. If it implements then the corresponding method will be executed.
If both attribute and binding listeners are configured,then binding listener will be executed first
followed by attribute listener.
Demo Program for HttpSessionBindingListener
SessionAttributeListener.java
web.xml:
SessionAttributeListener.java
Note:
We have to configure only AttributeListener and we are not required to configure binding listener.
FirstServlet.java
listener4
|-WEB-INF
|-web.xml
|-classes
|-FirstServlet.class
|-Dog.class
|-SessionAttributeListener.class
HttpSessionActivationListener
This method is called on each implementing object bound to the session just before serialization.
HttpSessionActivationListener
This method is called on each implementing object bound to the session just after
deserialization.
Summary of all Listeners:
Listener Purpose Corresponding Corresponding Corresponding Is
| Methods | Events | Event Methods | required |
|---|
Servlet
Request To listens life requestInitialized() ServletRequest getServletReque to
| Listener | cycle events of | requestDestroyed() | Event | st() | configure |
|---|---|---|---|---|---|
| request object. | getServletConte | web.xml | |||
| Servlet | i.e., request | ServletRequest xt() | Yes | ||
| Request | object creation | AttributeEvent | |||
| Attribute | & destruction | getName() | Yes | ||
| Listener | ServletContext getValue() | ||||
| To listens | attributeAdded() | Event | Yes | ||
| Servlet | events related | attributeReplaced() | getServletConte | ||
| Context | request scoped | attributeRemoved() | ServletContext xt() | Yes | |
| Listener | attributes | AttributeEvent |
getName() Yes
| Servlet | To listens life | contextInitialized() HttpSession | getValue() |
|---|---|---|---|
| Context | cycle events of | contextDestroyed() | Event |
| Attribute | context object. | getSession() |
Listener i.e., context
object creation
HttpSession & destruction
Listener
To listen events attributeAdded()
| related to | attributeReplaced() |
|---|---|
| context scoped | attributeRemoved() |
attributes
| To listens life | sessionCreated() |
|---|---|
| cycle events of | sessionDestroyed() |
session object.
i.e., session
object creation
& destruction
| HttpSession To listen events attributeAdded() | HttpSession | getName() | Yes | |
|---|---|---|---|---|
| Attribute | related to | attributeReplaced() | AttributeEvent getValue() | |
| Listener | session scoped | attributeRemoved() | HttpSession | not |
| attributes | BindingEvent | getName() | required | |
| HttpSession | getValue() | |||
| Binding | When ever we | HttpSession | not | |
| Listener | are adding or | BindingEvent | getSession() required |
removing or
| HttpSession replacing a | valueBound() | |
|---|---|---|
| Activation | particular type | valueUnbound() |
Listener of object in
session scope ,
to perform
certain activity
then we should
go for this
Listener
| If we want to | sessionWillPassivate HttpSession |
|---|
perform any () Event
| activity just | sessionDidActivate() |
|---|
before
serialization
and just after
de-serialization
in distributed
web-
applications
@WebListener Annotation in Servlet 3.0V:
This annotation is replacement for listener configuration in web.xml
Eg:
import javax.servlet.annotation.*;
@WebListener
public class RequestDemoListener implements ServletRequestListener
{
...
}
whenever we are using @WebListener annotation then we are not required to configure listener
in web.xml
- Every example is complete and compiles as-is
- Examples are grouped by topic
- Typing programs is the fastest way to learn servlets