Recipe7.2.Tracking Client Sessions


Recipe 7.2. Tracking Client Sessions

Problem

You need to keep track of the number of clients currently using your application.

Solution

Create a class that implements the HttpSessionListener interface, like the one shown in Example 7-2, that keeps count of the total number of active sessions.

Example 7-2. Session-counting listener
package com.oreilly.strutsckbk.ch07; import javax.servlet.ServletContext; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; public class SessionCounter implements HttpSessionListener {     public void sessionCreated(HttpSessionEvent event) {         ServletContext ctx = event.getSession( ).getServletContext( );         Integer numSessions = (Integer) ctx.getAttribute("numSessions");         if (numSessions == null) {             numSessions = new Integer(1);         }         else {             int count = numSessions.intValue( );             numSessions = new Integer(count + 1);         }         ctx.setAttribute("numSessions", numSessions);     }     public void sessionDestroyed(HttpSessionEvent event) {         ServletContext ctx = event.getSession( ).getServletContext( );         Integer numSessions = (Integer) ctx.getAttribute("numSessions");         if (numSessions == null) {             numSessions = new Integer(0);         }         else {             int count = numSessions.intValue( );             numSessions = new Integer(count - 1);         }         ctx.setAttribute("numSessions", numSessions);     } }

Declare your class in a listener element of your application's web.xml file. The listener element is supported by Version 2.3 or 2.4 of the Servlet specificationthe DTD must specify Version 2.3 or 2.4:

<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE web-app PUBLIC     "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"     "http://java.sun.com/dtd/web-app_2_3.dtd">      <web-app>   <display-name>Struts Cookbook - Chapter 7 Examples</display-name>      <listener>       <listener-class>com.oreilly.strutsckbk.ch07.SessionCounter       </listener-class>   </listener>   ... rest of web.xml

Discussion

In the Solution, any time a session is created, a counterstored in the servlet contextis incremented. Every time a session is destroyed, that counter is decremented. In practical terms, this custom class tracks the number of users currently using your application.

Web applications use HTTP sessions to maintain state between requests. The Servlet 2.3 specification added support for session listeners that receive notifications when any session is created or destroyed. You can write a class that receives these events by implementing the HttpSessionListener interface.

This interface defines two methods: sessionCreated( ) and sessionDestroyed( ). The container calls sessionCreated( ) after a session is created and sessionDestroyed( ) before the session is destroyed or invalidated. The container passes an HttpSessionEvent to each of these methods. This event object has a getSession( ) method that gives you a handle to the particular session.

Once you've got the HttpSession, you can do anything you want to with it. If the session was just created, you can add objects as session-scoped attributes. If destroyed, then you have the opportunity to dispose of session objects that require special handling. The HttpSession provides the getAttribute(), setAttribute( ), and removeAttribute( ) methods for manipulating session attributes. You can get a reference to your application's servlet context via the getServletContext( ) method.

See Also

While session listeners receive notification of session lifecycle events, servlet context listeners receive notification of application life-cycle events. These listeners are discussed in Recipe 7.1. If you want notifications specifically when objects are added to or removed from the session, use a session attribute listener discussed in Recipe 7.3.

If you are unfamiliar with servlet programming, you should check out Java Servlet Programming by Jason Hunter (O'Reilly). This classic work will give you all the information you need to create highly functional servlets and servlet-related classes.



    Jakarta Struts Cookbook
    Jakarta Struts Cookbook
    ISBN: 059600771X
    EAN: 2147483647
    Year: 2005
    Pages: 200

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net