| Java FAQ | ||
| JSP FAQ | ||
| Servlet FAQ | ||
XyzWs Servlet FAQ:
How to count active sessions in your web application?
Printer-friendly version |
Mail this to a friend
|
Advertisement
|
How to count active sessions in your web application?If you have unique users on a Web site and need to know who they are or need to get specific information to them, such as search results, then you should be using sessions. A session is a sort of temporary unique connection between the server and client by which the server keeps track of that specific user. That unique session can be used by web applications to display this content to one user and that content to the other user.
How to count active sessions in your web application? For a servlet container or application server which
supports Serlvet 2.3 speicification, we can have our session count class implementing
For example, we have MySessionCounter.java that implements
package com.xyzws.web.utils;
import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionEvent;
public class MySessionCounter implements HttpSessionListener {
private static int activeSessions = 0;
public void sessionCreated(HttpSessionEvent se) {
activeSessions++;
}
public void sessionDestroyed(HttpSessionEvent se) {
if(activeSessions > 0)
activeSessions--;
}
public static int getActiveSessions() {
return activeSessions;
}
}
Compile this code and drop the class file in the
To receive notification events, the implementation class
<!-- Listeners -->
<listener>
<listener-class>com.xyzws.web.utils.MySessionCounter</listener-class>
</listener>
For example, the following JSP code shows the total active session: <%@ page import="com.xyzws.utils.MySessionCounter"%> <html> ... Active Sessions : <%= MySessionCounter.getActiveSessions() %> ... </html> |