KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > javax > servlet > http > HttpSessionListener

javax.servlet.http
Interface HttpSessionListener

All Superinterfaces:
EventListener
See Also:
Top Examples, Source Code, HttpSessionEvent

public void sessionCreated(HttpSessionEvent se)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[246]Session Counter
By Anonymous on 2003/04/28 17:09:52  Rate
import javax.servlet.http.HttpSessionListener; 
 import javax.servlet.http.HttpSessionEvent; 
  
  
 public class SessionCounter implements HttpSessionListener  {  
  
  
     private static int activeSessions = 0; 
  
  
     /* Session Creation Event */ 
     public void sessionCreated ( HttpSessionEvent se )   {  
         activeSessions++; 
      }  
  
  
     /* Session Invalidation Event */ 
     public void sessionDestroyed ( HttpSessionEvent se )   {  
         if ( activeSessions  >  0 )  
             activeSessions--; 
      }  
  
  
     public static int getActiveSessions (  )   {  
         return activeSessions; 
      }  
  } 


[1982]Example for javax.servlet.http.HttpSessionListener
By meetshriram { at } gmail { dot } com on 2008/09/23 07:18:53  Rate
import java.util.Date; 
  
  
 import javax.servlet.http.HttpSession; 
 import javax.servlet.http.HttpSessionEvent; 
 import javax.servlet.http.HttpSessionListener; 
  
  
 public class SessionListen implements HttpSessionListener  {  
  
  
   private int sessionCount; 
  
  
   public SessionListen (  )   {  
     this.sessionCount = 0; 
    }  
  
  
   public void sessionCreated ( HttpSessionEvent se )   {  
     HttpSession session = se.getSession (  ) ; 
     session.setMaxInactiveInterval ( 60 ) ; 
     synchronized  ( this )   {  
       sessionCount++; 
      }  
     String id = session.getId (  ) ; 
     Date now = new Date (  ) ; 
     String message = new StringBuffer ( "New Session created on " ) .append (  
         now.toString (  )  ) .append ( "\nID: " ) .append ( id ) .append ( "\n" )  
         .append ( "There are now " ) .append ( "" + sessionCount ) .append (  
             " live sessions in the application." ) .toString (  ) ; 
  
  
     System.out.println ( message ) ; 
    }  
  
  
   public void sessionDestroyed ( HttpSessionEvent se )   {  
  
  
     HttpSession session = se.getSession (  ) ; 
     String id = session.getId (  ) ; 
     synchronized  ( this )   {  
       --sessionCount; 
      }  
     String message = new StringBuffer ( "Session destroyed" 
         + "\nValue of destroyed session ID is" ) .append ( "" + id ) .append (  
         "\n" ) .append ( "There are now " ) .append ( "" + sessionCount )  
         .append ( " live sessions in the application." ) .toString (  ) ; 
     System.out.println ( message ) ; 
    }  
  } 


public void sessionDestroyed(HttpSessionEvent se)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  

Popular Tags