Saturday, 14 March 2015

Listeners

LISTENERS

We have two types of programming languages
1.       Event driven programming languages
2.       Sequential programming languages
In sequential programming language system execute program in sequential manner.
In event driven programming language system execute program based on event.
Sun Micro System has created Servlet as a event driven programming. In Servlet then provided some event handler classes.
In Servlet we have following  event handler classes.
1.       ServletRequestListener
2.       ServletContextListener
3.       HttpSessionListener
public class MyServletRequestListener implements ServletRequestListener{

       public void requestDestroyed(ServletRequestEvent requestEvent) {
              System.out.println(requestEvent);
       }

       public void requestInitialized(ServletRequestEvent requestEvent) {
              System.out.println(requestEvent);
       }
      
}

public class MyServletContextListener implements ServletContextListener
{

       public void contextInitialized(ServletContextEvent contextEvent)
       {
       System.out.println("MyServletContextListener contextInitialized ");
       }
      
       public void contextDestroyed(ServletContextEvent contextEvent)
       {
       System.out.println("MyServletContextListener contextDestroyed()");
       }
}


public class MyHttpSessionListener implements HttpSessionListener
{
       public void sessionCreated(HttpSessionEvent sessionEvent)
       {
              System.out.println("MyHttpSessionListener sessionCreated");
       }

       public void sessionDestroyed(HttpSessionEvent sessionEvent)
       {
              System.out.println("MyHttpSessionListener sessionDestroyed");
       }

}

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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">
       <listener>
              <listener-class>
                     com.ibm.listeners.MyServletRequestListener
              </listener-class>
       </listener>
       <listener>
              <listener-class>
                     com.ibm.listeners.MyServletContextListener
              </listener-class>
       </listener>  
       <listener>
              <listener-class>
                     com.ibm.listeners.MyHttpSessionListener
              </listener-class>
       </listener>
</web-app>
































No comments:

Post a Comment