Thursday, 5 December 2013

HttpSessionListener , HttpSessionAttributeListener , ServletContextListener in java example


1. HttpSessionListener :- This interface may are notified of changes to the list of active sessions in a web application. To receive notification events, implementation class must be configured in the deployment descriptor (web.xml) for the web application. HttpSessionListener is used to keep track the the active session. When a new user login session created & add 1 in counter when invalidate logout minus 1 in counter.



create a web application with user login & user logout feature where you can see how this will be work.

implementing HttpSessionListener interface invoked at two method:-

1. public void sessionCreated(HttpSessionEvent httpSessionEvent)
    //Receives notification that a session has been created. LOGIN
    //Parameters: httpSessionEvent- containing the session.

2. public void sessionDestroyed(HttpSessionEvent httpSessionEvent)
   //Receives notification that a session is invalidated. LOGOUT
   //Parameters:httpSessionEvent - containing the session.

Sample Code :-

package com.javastoreroom.listener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.apache.log4j.Logger;

public class SessionListener implements HttpSessionListener {
 private static final Logger LOGGER = Logger.getLogger(SessionListener.class);
 private static int  activeSessionCounter;
 
 public static int getActiveSessionCounter() {
  return activeSessionCounter;
 }

 public static void setActiveSessionCounter(int activeSessionCounter) {
  SessionListener.activeSessionCounter = activeSessionCounter;
 }

 public void sessionCreated(HttpSessionEvent arg0) {
  activeSessionCounter++;
  LOGGER.info("session created ------ one add into counter -------> :"+activeSessionCounter);

 }

 public void sessionDestroyed(HttpSessionEvent arg0) {
  activeSessionCounter--;
  LOGGER.info("session destroy ----- remove one into counter -----> :"+activeSessionCounter);

 }

}

As we write above "implementation class must be configured in the deployment descriptor (web.xml)"


  com.javastoreroom.listener.SessionListener
 

now when you run your web application you can see in log :-
while user login :-session created ------ one add into counter -------> :1
While user Logout:-session destroy ----- remove one into counter -----> :0

2. HttpSessionAttributeListener:- This interface receiving notification events about HttpSession attribute changes.To receive these notification events, the implementation class must be either declared in the deployment descriptor or annotated with WebListener.HttpSessionAttributeListener is used to keep monitor session attribute. When a new user login session created & put username , id some other attribute in session to access globally in application. Using this interface we can check when attribute add , update and remove from session.

implementing HttpSessionAttributeListener interface invoked at three method:-

1. public void attributeAdded(HttpSessionBindingEvent bindingEvent)
    //Receives notification that an attribute has been added to a session. LOGIN & set USERNAME IN SESSION
    //Parameters:bindingEvent- containing the session and the name and value of the attribute that was added

2. public void attributeRemoved(HttpSessionBindingEvent bindingEvent)
    //Receives notification that an attribute has been removed from a session.
    //Parameters:bindingEvent-containing the session and the name and value of the attribute that was removed

3. public void attributeReplaced(HttpSessionBindingEvent bindingEvent)
    //Receives notification that an attribute has been replaced in a session.
    //Parameters:bindingEvent- containing the session and the name and (old) value of replaced with new one.

Sample Code:-
package com.javastoreroom.listener;

import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import org.apache.log4j.Logger;

public class SessionAttributeListener implements HttpSessionAttributeListener {
 public static final Logger LOGGER = Logger.getLogger(SessionAttributeListener.class);

 public void attributeAdded(HttpSessionBindingEvent bindingEvent) {
  String attributeName = bindingEvent.getName();
  Object object = bindingEvent.getValue();
              LOGGER.info("attribute added in session -->" + attributeName + "&"+ object);
 }

 public void attributeRemoved(HttpSessionBindingEvent bindingEvent) {
  String attributeName=bindingEvent.getName();
  Object object=bindingEvent.getValue();
  LOGGER.info("attribute removed in session -->" + attributeName + "&"+ object);

 }

 public void attributeReplaced(HttpSessionBindingEvent bindingEvent) {
  String attributeName=bindingEvent.getName();
  Object object=bindingEvent.getValue();
  LOGGER.info("attribute replace in session -->" + attributeName + "&"+ object);
 }
}

"implementation class must be configured in the deployment descriptor (web.xml)"


  com.javastoreroom.listener.SessionAttributeListener
 

3.ServletContextListener:-This interface receive notifications about changes to the servlet context of the web application they are part of. To receive notification events, the implementation class must be configured in the deployment descriptor for the web application.If you want to run your code before the web application is start.Here you want to start sending notification when application start either user login or not.

implementing ServletContextListenerinterface invoked at two method:-

1. public void contextInitialized(ServletContextEvent servletContextEvent)
    //Notification that the web application initialization process is starting. All ServletContextListeners are notified of context initialization before any filter or servlet in the web application is initialized.

2. public void contextDestroyed(ServletContextEvent servletContextEvent)
    //Notification that the servlet context is about to be shut down. All servlets and filters have been destroy()ed before any //ServletContextListeners are notified of context destruction. 

Sample Code:-

package com.javastoreroom.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

import org.apache.log4j.Logger;

public class applicationListener implements ServletContextListener {
 public static final Logger LOGGER = Logger.getLogger(SessionAttributeListener.class);

 @Override
 public void contextDestroyed(ServletContextEvent arg0) {
  LOGGER.info("context destory when stop ");
  
 }

 @Override
 public void contextInitialized(ServletContextEvent arg0) {
  LOGGER.info("context started before aplication started ");
  
 }
}

When you start a tomcat server see log "context started before aplication started" line will be shown before application start.

:) Enjoy JAVA Love it

No comments:

Post a Comment