How to retrieve information about online users

by Michela Frigerio
11,537 views Published on Oct 24, 2011
Applies to: All versions
Table of contents

This article discusses how to get the number of users which are currently navigating a Web application. WebRatio gives you the possibility to easily add this feature to your Web application. Just follow these steps:

  • Create a new Java project, choosing the New -> Project -> Java Project, and name it "<MyWebApplication>Components"
  • Create a new package named "com.<companyName>.session"
  • Create a new Java class named "CheckOnlineUsers". The "CheckOnlineUsers" implements a HTTPSessionListener which is an object that listens to the active sessions, allowing to do different operations when a session is created and when a session is destroyed. In particular, the proposed solution is to store in a Set references of the active sessions. Every time a session is created, it is stored in this set and, viceversa, every time a session is destroyed it is removed from the set. You have also to add new methods to the class in order to be able to get the size of the set(which represents the current online users number) and the array containing the identifiers of all the online users. This is the class code:

import java.util.ArrayList;

import java.util.Collections;

import java.util.HashSet;

import java.util.Iterator;

import java.util.List;

import java.util.Map;

import java.util.Set;

import javax.servlet.ServletContext;

import javax.servlet.http.HttpSession;

import javax.servlet.http.HttpSessionEvent;

import javax.servlet.http.HttpSessionListener;

import org.apache.commons.lang.ObjectUtils;

import com.webratio.rtx.RTXConstants;

import com.webratio.struts.WRGlobals;





public class CheckOnlineUsers implements HttpSessionListener {



	public static final String KEY = CheckOnlineUsers.class.getName();



	private Set activeSessions = Collections.synchronizedSet(new HashSet());





	public void sessionCreated(HttpSessionEvent se) {

		HttpSession session = se.getSession();

		registerInServletContext(session.getServletContext());

		synchronized (activeSessions) {

			activeSessions.add(session);

		}

	}



	public void sessionDestroyed(HttpSessionEvent se) {

		synchronized (activeSessions) {

			activeSessions.remove(se.getSession());

		}

	}



	private void registerInServletContext(ServletContext servletContext) {

		if (servletContext.getAttribute(KEY) == null) {

			servletContext.setAttribute(KEY, this);

		}

	}



	public int getActiveSessionCount() {

		return activeSessions.size();

	}



	public List getActiveUsers() {

		synchronized (activeSessions) {

			List activeUsers = new ArrayList(activeSessions.size());

			for (Iterator i = activeSessions.iterator(); i.hasNext();) {

				HttpSession session = (HttpSession) i.next();

				Object sessionCtx = session

						.getAttribute(WRGlobals.SESSION_CONTEXT_KEY);

				if (sessionCtx instanceof Map) {

					Object userCtxParam = ((Map) sessionCtx)

							.get(RTXConstants.CURRENT_USER_CTX_PARAM_KEY);

					if (userCtxParam != null) {

						activeUsers.add(ObjectUtils.toString(userCtxParam));

					}

				}

			}

			return activeUsers;

		}

	}



}

  • Right click on the Java project and choose the Build Path -> Configure Build Path command. In the opening dialog, click the Add Library button and choose the WebRatio Library option. The check also the "WebRatio Runtime Framework" and the *WebRatio Struts Extension" options. Click the Finish button and then the "OK" button.
  • Right click on the Java project and choose the Build Path -> Configure Build Path command. In the opening dialog, choose the Java Compiler option on the left menu. Check the "Enable project specific settings" option and the set the "Compiler Compliance Level" to "1.4". This is necessary because all the WebRatio runtime engine and all the WebRatio units use this compliance level.
  • Right click on the Java project and choose the Export -> JAR file command. Choose the lib folder of your Web Project as Export Destination. In this way you are going to add a jar in your Web Project with the compiled classes.
  • Copy the web.xml file of your generated Web application in the WebContent/WEB-INF directory of your Web Project. Open it and add the following lines, which declare the usage of a listener

       <listener>

	   <listener-class>

		com.<companyName>.session.CheckOnlineUsers

           </listener-class>

       </listener>

  • Add to your Web Project a new page called "Users Statistics". Add a Script Unit and a MultiDataUnit to this page. Look at the image below for an example.
  • Write the Groovy Script for the Script Unit. The script has to retrive the SessionListener object and the information stored in it (the array of user oids and the number of active sessions). Here is the code

//outputs=onlineUsers|userOids

import javax.servlet.http.HttpServletRequest

import com.webratio.rtx.RTXConstants

import com.webratio.session.CheckOnlineUsers



HttpServletRequest request = localContext.get(RTXConstants.HTTP_SERVLET_REQUEST_KEY)

CheckOnlineUsers c = (CheckOnlineUsers) request.getSession().getServletContext()

                          .getAttribute("com.webratio.session.CheckOnlineUsers")

def onlineUsers = c.getActiveSessionCount()

def userOids = c.getActiveUsers()



return ["onlineUsers" : onlineUsers, "userOids": userOids.toArray()]



  • You can also create a new Unit Template for the Script Unit in order to write the current online users number. From the main menu choose File -> New.. -> Layout Template. In the opening dialog choose Unit for the template type and then the Script Unit. Choose a name for the template and click on the Finish button. Here is the code for the template

#?delimiters [%, %], [%=, %]

[%setHTMLOutput()%]

<wr:Frame>

<div class="plain <wr:StyleClass/>">

  <div class="plain ScriptUnit">

      There are <c:out value="${<wr:UnitId/>.result.onlineUsers}"/> online users.

  </div>

</div>  

</wr:Frame>

  • Assign the template to the Script Unit modeled in the "User Statistics" page and generate the Web Project.

You can see an example of the final result in the following image.