How can I find out the number of live sessions within my servlet engine using either JSP or servlets?

Volker Stolz

There is no easy way to this as all the required functions have been deprecated in JSP 1.1 for security reasons. [FAQ Manager NOTE - For earlier JSP users, scroll to end for a working answer for there.]

However, you can use HttpSessionBindingListeners to track live sessions, e.g.:

class SqlNotifier implements HttpSessionBindingListener {
  
  protected Connection con;
  
  public SqlNotifier(Connection con){
    this.con = con;
  }
  
  public void valueBound(HttpSessionBindingEvent e) {
    HttpSession session = e.getSession();
    // log creation to a database
    ....
    }
  }
  
  public void valueUnbound(HttpSessionBindingEvent e) {
    HttpSession session = e.getSession();
    // log destruction...
    ...
  }
}
and:
session.putValue("logon.sqlnotifier",new SqlNotifier(sql));
upon creation of a new session.

[FAQ Manager addition - The following was submitted by both Peter Wang and Roger Valade for earlier JSP versions. It does not run in the 3.1 version of Tomcat.]

<%@ page import="java.util.*" %>
<html>
<head>
  <meta http-equiv="Refresh" content=90>
  <title>Active Sessions</title>
</head>

<%!
    final String getActiveSessionData( final HttpServletRequest request )
    {
      final StringBuffer data = new StringBuffer();
      int counter = 0;
      final HttpSession        curSession   = request.getSession( true );
      final String             curSessionId = curSession.getId();
      final HttpSessionContext context      = curSession.getSessionContext();
      for ( final Enumeration sessionIds = context.getIds(); sessionIds.hasMoreElements(); )
      {
        final String sessionId = (String) sessionIds.nextElement().toString();
//        if ( curSessionId == sessionId )
//        {
//          continue;
//        }
        String userId     = sessionId;
        long   creation   = 0;
        long   lastAccess = 0;
       
          final HttpSession session = context.getSession( sessionId );
          userId     = (String)session.getValue("UserID");
          creation   = session.getCreationTime();
          lastAccess = session.getLastAccessedTime();
    Calendar currentTime = Calendar.getInstance(TimeZone.getTimeZone("EST"));
          currentTime.setTime( new Date(creation));
    Date estCreatTime =currentTime.getTime();
    currentTime.setTime( new Date(lastAccess));
    Date estLastAccess   = currentTime.getTime();
        data.append( " <tr><td>" )
            .append( ++counter )
            .append( "</td><td>" )
            .append( userId )
            .append( "</td><td>" )
            .append( creation==0 ? "Not Available" : estCreatTime.toString() )
            .append( "</td><td>" )
            .append( lastAccess==0 ? "Not Available" :estLastAccess.toString())
   .append( "</td></tr>" );
      }
data.append("Hi");
      return data.toString();
    }
%>

<body>
  <div class="PageTitle">Active Sessions:</div>
  <hr color="#000000" noshade>
  <table class="DataTable" width="100%" cellspacing=0
cellpadding=0 border=1>
    <tr>
      <th>#</th>
      <th>User</th>
      <th>Creation Time</th>
      <th>Last Access</th>
    </tr>
    <% out.println( getActiveSessionData( request )
);%>
  </table>
  <form><input type="submit" value="Refresh"
onClick="location.reload();"></form>
</body>
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact