FileDocCategorySizeDatePackage
SessionCounterListener.javaAPI DocExample1475Tue Feb 28 11:34:06 GMT 2006com.ora.jsp.servlets

SessionCounterListener

public class SessionCounterListener extends Object implements HttpSessionListener
This class manages a counter for the number of active sessions in an application. The counter is made available to the rest of the application as a servlet context attribute of type int[] with one element.
author
Hans Bergsten, Gefion software
version
1.0

Fields Summary
private static final String
COUNTER_ATTR
Constructors Summary
Methods Summary
private int[]getCounter(javax.servlet.http.HttpSessionEvent hse)
Returns the counter held in the session scope, or a new counter if it doesn't exist.

 
	HttpSession session = hse.getSession(); 
	ServletContext context = session.getServletContext(); 
	int[] counter = (int[]) context.getAttribute(COUNTER_ATTR); 
	if (counter == null) { 
	    counter = new int[1]; 
	    context.setAttribute(COUNTER_ATTR, counter); 
	} 
	return counter; 
    
public voidsessionCreated(javax.servlet.http.HttpSessionEvent hse)
Increments the counter held in the session scope.


                 
         
	int[] counter = getCounter(hse); 
	counter[0]++; 
    
public voidsessionDestroyed(javax.servlet.http.HttpSessionEvent hse)
Decrements the counter held in the session scope.

 
	int[] counter = getCounter(hse); 
	counter[0]--;