Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · ASP.NET Newsletter Application · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Newsletter
Enter your email address to receive full length articles at Stardeveloper:


Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (43)
  ADO (16)
  ADO.NET (11)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)

Main Category  Other
  Website Maintenance (3)
Log In
UserName Or Email:

Password:

Auto-Login:

Hosted by Securewebs.com
 
Home : J2EE : Servlets : Counting Active Users using JSP
 
Read full length articles at Stardeveloper using Twitter Follow on Twitter Facebook Facebook fan page Email Get Articles via Email RSS Get Articles via RSS Feed

Counting Active Users using JSP

by Faisal Khan.

Overview
Ever wondered how many users are viewing your web site at this moment? well this article seems to answer that. We will learn how to count active users using one session listener class and a JSP page. Once you have read this article, you'll be able to see the number of active sessions on your web site. This is all fun, so keep reading.

What is a Session ?
A session is a sort of temporary unique connection between the server and client ( browser ) 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.

As part of Servlet 2.3 specification, we can now make use of session creation and destruction events. Our listener object will be called every time a session is created or destroyed by the server. This is very useful. One use of these events is to count active users which we are going to do in this article.

How to count active Sessions ?
Simply increment a private integer variable every time a session is created and decrement it every time a session is destroyed. We'll learn more about it when we create our session listener class.

What is required ?
A Servlet container or application server which supports Servlet 2.3 specification. Examples of such application servers are Tomcat 4.1, Orion 1.6.0, Resin 2.1.4, BEA Web Logic 6.1 etc.

SessionCounter.java
Create a new Java source file and save it as SessionCounter.java in the /WEB-INF/classes/com/stardeveloper/web/listener/ folder of your web application. Now copy and paste the following code in it :

/*
	SessionCounter.java
*/
package com.stardeveloper.web.listener;

import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionEvent;

public class SessionCounter 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;
	}
}

Explanation
First line is the package statement. Next are two import statements which import the HttpSessionListener interface and HttpSessionEvent class.

 package com.stardeveloper.web.listener;

import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionEvent;

Then we declare our SessionCounter class and implement HttpSessionListener interface. HttpSessionListener interface is present in the javax.servlet.http package and our class *has to* implement this interface if we want it to be told of session creation and destruction events. This interface contains just two simple methods relative to the session creation and destruction events as we'll see in a moment. To learn more about HttpSessionListener interface, click here.

 public class SessionCounter implements HttpSessionListener {

Next we declare a class level ( static ) private integer variable which will hold the active session count for our SessionCounter class.

	private static int activeSessions = 0;

Now we will implement the two methods of HttpSessionListener interface. sessionCreated() method will be called by the server every time a session is created and sessionDestroyed() method will be called every time a session is invalidated or destroyed.

In sessionCreated() method we increment our internal integer variable activeSessions. And in sessionDestroyed() method we decrement the value of activeSessions.

	public void sessionCreated(HttpSessionEvent se) {
		activeSessions++;
	}

	public void sessionDestroyed(HttpSessionEvent se) {
		if(activeSessions > 0)
			activeSessions--;
	}

We have implemented both the methods of HttpSessionListener interface, now the only thing remains is to create a public static method to be called by our JSP page to get the activeSessions count.

	public static int getActiveSessions() {
		return activeSessions;
	}

Now save SessionCounter.java file and compile it.

Sessions.jsp
Now create a new JSP page in any folder you like accessible by your server and give it any name. For example's sake we call it "Sessions.jsp" and save it in / main folder. Copy and paste the following code in it :

<%-- Sessions.jsp --%>
<%@ page import="com.stardeveloper.web.listener.SessionCounter" %>
<html>
<head>
	<title>Active Sessions</title>
</head>
<body>
	<p align="center">
	Active Sessions : <%= SessionCounter.getActiveSessions() %>
	</p>
</body>
</html>

Explanation
There are only two important lines in the Sessions.jsp code above, rest is all client-side HTML. First line is the "page" directive containing an "import" attribute with the value of com.stardeveloper.web.listener.SessionCounter class.

<%@ page import="com.stardeveloper.web.listener.SessionCounter" %>

Next important line comes when we call the public static method ( getActiveSessions() ) of our SessionCounter class. It simply displays the number of active sessions in the browser.

	Active Sessions : <%= SessionCounter.getActiveSessions() %>

Next we'll learn what we have to do with the /WEB-INF/web.xml file in order to tell the application server that we have this SessionCounter listener class so kindly call it's session event methods every time a session is created or destroyed.

Web.xml
Edit your /WEB-INF/web.xml file and make sure it contains <listener> and <listener-class> tags as shown below, if there is no /WEB-INF.web.xml file then create it, copy and paste the following text in it :

<!-- Web.xml -->
<?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/j2ee/dtds/web-app_2.3.dtd">

<web-app>

	<!-- Listeners -->
	<listener>
		<listener-class>
		com.stardeveloper.web.listener.SessionCounter
		</listener-class>
	</listener>

</web-app>

If you already have a /WEB-INF/web.xml file then simple copy and paste following text in the middle of your web.xml file :

	<!-- Listeners -->
	<listener>
		<listener-class>
		com.stardeveloper.web.listener.SessionCounter
		</listener-class>
	</listener>

Now stop and restart your application server and call your Sessions.jsp page. You should be able to see number of active sessions.


 ( 1 Remaining ) Next

Comments/Questions ( Threads: 9, Comments: 16 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. sessionDestroyed ( 1 Reply )
  2. Somethings not working
  3. How to count the number of Users visited by Website and display it? ( 1 Reply )
  4. Re:Counting Users
  5. Listener not accepted in web.xml ( 1 Reply )
  6. Problem with sessionDestroyed method ( 2 Replies )
  7. Getting an attribute from the session from the SessionEvent doesn't work in Tomcat
  8. reg error
  9. This Doesn't Work in Orion ( 2 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed.

 
© 1999 - 2010 Stardeveloper.com, All Rights Reserved.