Listener in JAVA

Listener class listen the event which associated with the application. This can be context or session of the application. Listener gives us a great control over application without touching architecture of the application.

Listener interface are ServletContextListener and HttpSessionListener.

How Listener interface works

Whenever any event register to the application, listener interface get notification with new event, destroy event or modified event. We can use method of Listener interface in our class to get notification.

Listener class should be defined in web.xml file in web application.

I will explain you Listener with examples

1. ServletContextListener

2. HttpSessionListener

 

1. ServletContextListener

ServletContextListener works when new context is registered to web server.

ServletContextListener methods

a. contextInitialized()

b. contextDestroyed()

a. contextInitialized()

When we start web server contextInitialized() get event on ServletContext is created and contextInitialized() is executed.

b. contextDestroyed()

contextDestroyed() is called when ServletContext is destroyed.

Example of ServletContextListener

web.xml

<web-app>
	<display-name>Listener</display-name>

	 <listener>
          <listener-class>com.listener.MyContextListener</listener-class>
	 </listener>

</web-app>

Implement ServletContextListener in your class

MyContextListener.java

package com.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class MyContextListener implements ServletContextListener{

    public void contextInitialized(ServletContextEvent event) {
        System.out.println("New Context is created");
    }

    public void contextDestroyed(ServletContextEvent event) {
        System.out.println("Context is Destroyed");
    }

}

You can check output at console of tomcat server for print line when tomcat is started

2. HttpSessionListener

HttpSessionListener works when new session is created or destroyed.

HttpSessionListener methods

a. sessionCreated()

b. sessionDestroyed()

a. sessionCreated()

sessionCreated() method is called when new session is registered to the application.

b. sessionDestroyed()

sessionDestroyed() is called when session is destroyed from application.

Example of HttpSessionListener

web.xml

<web-app>
	<display-name>Listener</display-name>

	 <listener>
          <listener-class>com.listener.MySessionListener</listener-class>
	 </listener>

</web-app>

MySessionListener.java

package com.listener;

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class MySessionListener implements HttpSessionListener{

    int newActiveSession=0;
    int totalSessionCreated=0;

    public void sessionCreated(HttpSessionEvent event) {
        System.out.println("New Session is created");
        newActiveSession++;
        totalSessionCreated++;
        System.out.println("Current Active Session :="+newActiveSession);

        System.out.println("Total No Session Created :="+totalSessionCreated);
    }

    public void sessionDestroyed(HttpSessionEvent event) {
        System.out.println("Session is destroyed");
        newActiveSession--;
    }

}

Whenever new session create, it will print in console of tomcat as output

Tags:

Bookmark  

 

Leave a Reply

Security Code:

 

  Random Post