Friday 15 June 2012

ServletListeners

  • Listener is one of the most popular technologies used in the J2EE web application
  • It is part of the Java Servlet as defined in Servlet 2.3 but they have their own specific functionalities.
  • They are 8 Listeners
    • ServletContextListener
    • ServletContextAttributeListener
    • RequestListener
    • RequestAttributeListener
    • HttpSessionListener.
    • HttpSessionAttributeListener
    • HttpSessionActivationListener 
    • HttpSessionBindingListener 
  • What is Listener
Listener is basically pre-defined interfaces that are available for developers 
in the application lifecycle to achieve some tasks especially when dealing
with the ServletContext as well as HttpSession objects


ServletContextListener

  • Server creates servletContext object when we deploy the project in the server and server destroys the ServeltContext object when we un-deploy the project

  • This section contains event listener methods that are called by the servlet container when a servlet context event 
  • The ServletContextListener interface specifies the following methods
    • void contextInitialized(ServletContextEvent sce)
      • The servlet container calls this method to notify the listener that the servlet context has been created and the application is ready to process requests
    •  void contextDestroyed(ServletContextEvent sce)
      • The servlet container calls this method to notify   
        the listener that the application is about to be
        shut down.


  • The servlet container creates a javax.servlet.ServletContextEvent object that is input for calls to ServletContextListener methods. The ServletContextEvent class includes the following method, which your listener can call:
    • ServletContext getServletContext()
      • Use this method to retrieve the servlet context object that was created or is about to be destroyed, from which you can obtain information as desired. See "Introduction to Servlet Contexts" for information about thejavax.servlet.ServletContext interface
  • Example program to describe the ServletContextListener

  • public class event1 implements ServletContextListerner
    {
    public void contextInitialized(ServletContextEvent se)
    {
    System.out.println(“servletContext is created”);
    }
    public void contextDestroyed(ServletContextEvent sc)
    {
    System.out.println(“servletContext is destroyed”);
    }
    }
    web.xml
      <listener>
      <listener-class>lrv1</listener-class>
      </listener>

  • ServletContextAttributeListener.
  • For ServletContextAttributeListener event is ServletContextAttributeEvent.
  • So, when we add attribute to ServletContext one event is raised that event is known as ServletcontextAttributeEvent
  • The ServletContextAttributeListener interface specifies the following methods:
    • void attributeAdded(ServletContextAttributeEvent obj)
      • The servlet container calls this method to notify the listener that an attribute was added to the servlet context.
    • void attributeRemoved(ServletContextAttributeEvent obj)
      • The servlet container calls this method to notify the listener that an attribute was removed from the servlet context.
    • void attributeReplaced(ServletContextAttributeEvent obj)
      • The servlet container calls this method to notify the listener that an attribute was replaced in the servlet context
  • The container creates a javax.servlet.ServletContextAttributeEvent object that is input for calls to ServletContextAttributeListener methods. 
  • The ServletContextAttributeEvent class includes the following methods, which your listener can call
    • String getName()
      • Use method this to get the name of the attribute that was added, removed, or replaced.
    • Object getValue()
      • Use this method to get the value of the attribute that was added, removed, or replaced. In the case of an attribute that was replaced, this method returns the old value, not the new value
  • Example Program

    public class event2 implements ServletContextAttributeListener
    {
    public void attributeAdded(ServletContextAttributeEvent scae)
    {
    System.out.println(“servletContextAttributeEvent is added”);
    }
    public void attributeRemoved(ServletContextAttributeEvent scae)
    {
    System.out.println(“ServletContextAttributeEvent is removed”);
    }
    public void attributeReplaced(ServletContextAttributeEvent scae)
    {
    System.out.println(“servletContext Attribute is replaced”);
    }
    }
web.xml
<listener>
<listener-class>lrv2</listener-class>
</listener>

Note: In java all listeners are interface and all events are classes. so for each listener there exists one event.

I hope you understand above explanation now see anther listeners 


 ServletRequestListener and ServletRequestAttributeListener

  • Now for ServletRequestListener event is ServletRequestEvent
  • when Server creates the ServletRequest object ServletReqeustEvent is raised then the Methods ServletRequestListener will be executed.
  • In the same way when we add or delete attribute to ServletRequest object  ServletRequestAttributeEvent will be raised then the methods in ServletRequestAttributeListener will be executed.
  • Methods in ServletRequestListener Interface
    • public void requestInitialized(ServletRequestEvent obj)
    • public void requestDestoyed(ServletRequestEvent obj)
  • Now for ServletRequestAttributeListener event is ServletRequestAttributeEvent
  • Methods in ServletRequestAttributeListerner
    • void attributeAdded(ServletRequestAttributeEvent obj)
    • void attributeReplaced(ServletRequestAttributeEvent obj)
    • void attributeRemoved(ServletRequestAttributeEvent obj)
  • Example program for above explanation

import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
public class Example implements ServletRequestAttributeListener, ServletRequestListener {
    public Example() {
       System.out.println("Object is created....");
    }
    public void requestDestroyed(ServletRequestEvent arg0) {
       System.out.println("Request object is deleted.....");
    }
    public void attributeAdded(ServletRequestAttributeEvent arg0) {
       System.out.println("Attribute is added to request obj");
    }
    public void attributeRemoved(ServletRequestAttributeEvent arg0) {
       System.out.println("Attribute is removed to request obj");
    }
    public void attributeReplaced(ServletRequestAttributeEvent arg0) {
           System.out.println("Attribute is replaced to request obj");
    }
    public void requestInitialized(ServletRequestEvent arg0) {
       System.out.println("Request object is created......");
    }
}

web.xml
<listener>
<listener-class>Example</listener-class>
</listener>


HttpSessionListener and HttpSessionAttributeListener

  • HttpSessionListener event is HttpSessionEvent 
  • that means when we crate HttpSession, HttpSessionEvent will be raised and then methods in HttpSessionListener will be executed...
  • In the same way when we add attributes to HttpSession, HttpSessionAttributeEvent is raised and then methods in HttpSessionAttributeListener will be exeucted...
  • Methods in HttpSessionListener
    • void sessionCreated(HttpSessionEvent hse)
    • void sessionDestroyed(HttpSessionEvent hse)
  • Methods in HttpSessionAttributeListener
    • void attributeAdded(HttpSessionAttributeEvent obj)
    • void attributeRemoved(HttpSessionAttributeEvent ob)
    • void attributeReplaced(HttpSessionAttributeEven ob)
  • The container creates a javax.servlet.http.HttpSessionEvent object that is input for calls to HttpSessionListener methods. The HttpSessionEvent class includes the following method, which your listener can call
    •  HttpSession getSession()
  Example Program for HttpSessionListener and HttpSessionAttribueListener
         

public class event3 implements HttpSessionListener,HttpSessionAttributeListener
{
Public void sessionCreated(HttpSessionEvent hse)
{
System.out.println(“session is created”);
}
Public void sessionDestroyed(HttpSessionEvent hse)
{
System.out.println(“session is destroyed”);
}
Public void attributeCreated(HttpSessionBindingEvent hsbe)
{
System.out.println(“attribute is created”);
}
Public void attributeReplaced(HttpSessionBindingEvent hsbe)
{
System.out.println(“attribute is replaced”);
}
Public void attributeDestroyed(HttpSessionBindingEvent hsbe)
{
System.out.println(“attributed is removed”);
}
}


Web.xml
<listener>
<listener-class> event3 </listener-class>
</listener>



HttpSessionActivationListener and  HttpSessionBindingListener  

Methods in HttpSessionActivationListener Interface





No comments:

Post a Comment