Servlet Listener is used for listening to events in a web container, such as when you create a session or place an attribute in a session or if you passivate and activate in another container, to subscribe to these events you can configure listener in web.xml, for example, HttpSessionListener.
Listeners get triggered for an actual physical request that can be attached to events in your app server .With listeners, you can track application-level, session-level, life-cycle changes, attribute changes etc.
You can monitor and react to events in a servlet's life cycle by defining listener objects whose methods get invoked when lifecycle events occur.
Event
Event is the occurrence of something, an event can be the initialization of the application, destroying an application, request from the client, creating/destroying a session, attribute modification in session etc.Servlet API
This provides different types of Listener interfaces that we can implement and configure in web.xml.
Servlet API provides following event objects.
Event Object
|
Description
|
javax.servlet.AsyncEvent
|
Event that gets fired when the asynchronous operation initiated on a ServletRequest has completed, timed out, or produced an error.
|
javax.servlet.http.HttpSessionBindingEvent
|
Events of this type are either sent to an object that implements HttpSessionBindingListener when it is bound or unbound from a session, or to a HttpSessionAttributeListener that has been configured in the web.xml when any attribute is bound, unbound or replaced in a session.
The session binds the object by a call to HttpSession.setAttribute and unbinds the object by a call to HttpSession.removeAttribute. We can use this event for cleanup activities when object is removed from session. |
javax.servlet.http.HttpSessionEvent
|
Event notifications for changes to sessions within a web application.
|
javax.servlet.ServletContextAttributeEvent
|
Event notifications about changes to the attributes of the ServletContext of a web application
|
javax.servlet.ServletContextEvent
|
Event notifications about changes to the servlet context of a web application
|
javax.servlet.ServletRequestEvent
|
Event notification about lifecycle of ServletRequest.
|
javax.servlet.ServletRequestAttributeEvent
|
Event class for notifications of changes to the attributes of the servlet request in an application
|
Servlet API provides following Listener interfaces :-
Listener Object
|
Description
|
javax.servlet.AsyncListener
|
Listener that will be notified in the event that an asynchronous operation initiated on a ServletRequest to which the listener had been added has completed, timed out, or resulted in an error
|
javax.servlet.ServletContextListener
|
Interface for receiving notification events about ServletContext lifecycle changes
|
javax.servlet.ServletContextAttributeListener
|
Interface for receiving notification events about ServletContext attribute changes
|
javax.servlet.ServletRequestListener
|
Interface for receiving notification events about requests coming into and going out of scope of a web application
|
javax.servlet.ServletRequestAttributeListener
|
Interface for receiving notification events about ServletRequest attribute changes
|
javax.servlet.http.HttpSessionListener
|
Interface for receiving notification events about HttpSession lifecycle changes
|
javax.servlet.http.HttpSessionBindingListener
|
Causes an object to be notified when it is bound to or unbound from a session.
|
javax.servlet.http.HttpSessionAttributeListener
|
Interface for receiving notification events about HttpSession attribute changes
|
javax.servlet.http.HttpSessionActivationListener
|
Objects that are bound to a session may listen to container events notifying them that sessions will be passivated and that session will be activated. A container that migrates session between VMs or persists sessions is required to notify all attributes bound to sessions implementing HttpSessionActivationListener.
|
Example : Counting logged in users Using HttpSessionEvent and HttpSessionListener
Index.html
<form action="login">
Name:<input type="text" name="userName"><br>
Password:<input type="password" name="password"><br>
<input type="submit" value="Login"/>
</form>
Login.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class First extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String username =request.getParameter("userName");
out.print("Welcome "+ userName);
HttpSession session=request.getSession();
session.setAttribute("userName",userName);
ServletContext ctx=getServletContext();
int loggedInUsers=(Integer)ctx.getAttribute("loggedInUsers");
out.print("<br>Total logged in users= "+loggedInUsers);
out.close();
}
}
MyListener.java
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
@WebListener
publicclassMyListenerimplementsHttpSessionListener{
ServletContext ctx=null;
static int loggedInUsers=0;
publicvoid sessionCreated(HttpSessionEvent sessionEvent){
loggedInUsers++;
ctx=e.getSession().getServletContext();
ctx.setAttribute("loggedInUsers", loggedInUsers);
}
publicvoid sessionDestroyed(HttpSessionEvent sessionEvent){
loggedInUsers--;
ctx.setAttribute("loggedInUsers",loggedInUsers);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="WebApp_ID"version="3.0">
<display-name>Servlet Listener</display-name>
<listener>
<listener-class>MyListener</listener-class>
</listener>
</web-app>
http://www.journaldev.com/1945/servletcontextlistener-servlet-listener-example
http://www.javatpoint.com/Event-and-Listener-in-Servlet
Thanks a lot! You made a new blog entry to answer my question; I really appreciate your time and effort.
ReplyDeletejava training and placement in chennai |
java j2ee training placement in chennai