Java Examples – How do I count number of online users?

How do I count number of online users?

Category: javax.servlet, viewed: 10K time(s).

When you have a web application you might want to know how many users are currently online or connected to your website. If you have visited some of web online forums you can see; usually on the first page; the list of their online users or maybe just the number of currently online users.

How do we know / count how many sessions or users are currently connected to our website. Do you care to know? Let’s see what’s Java Servlet API offers us on this matter.

Servlet API has an interface javax.servlet.http.HttpSessionListener, an implementation of this interface will have the ability to be notified by the servlet engine at anytime when a new session is created or destroyed.

This interface has two methods to be implemented; these methods are sessionCreated(HttpSessionEvent se) and sessionDestroyed(HttpSessionEvent se). These method will be called as a notification that a new session was created and the session was about to be destroyed respectively.

Now let’s create our session listener. The code below is what our class is going to be implemented.

01.
package org.kodejava.servlet.examples;
02.

03.
import javax.servlet.http.HttpSessionEvent;
04.
import javax.servlet.http.HttpSessionListener;
05.
import javax.servlet.http.HttpSession;
06.
import java.util.List;
07.
import java.util.ArrayList;
08.

09.
public class SessionCounter implements HttpSessionListener
10.
{
11.
private List sessions = new ArrayList();
12.

13.
public SessionCounter()
14.
{
15.
}
16.

17.
public void sessionCreated(HttpSessionEvent event)
18.
{
19.
HttpSession session = event.getSession();
20.
sessions.add(session.getId());
21.

22.
session.setAttribute(“counter”, this);
23.
}
24.

25.
public void sessionDestroyed(HttpSessionEvent event)
26.
{
27.
HttpSession session = event.getSession();
28.
sessions.remove(session.getId());
29.

30.
session.setAttribute(“counter”, this);
31.
}
32.

33.
public int getActiveSessionNumber()
34.
{
35.
return sessions.size();
36.
}
37.
}
To display information of current online users we need to create a simple JSP page. This JSP file will get the number of online user from HttpSession attribute named counter that we set in our listener above.

Session Counter

Number of online user(s):

The final step to make the listener working is to register it in the web.xml file. Below is the example how to register the listener in web.xml.

org.kodejava.servlet.examples.SessionCounter

Advertisements

About ege's press.

Few things. I'm lizard king i can do anything. Let's start this shit. mpla mpla mpla de mas pernei gia polla. (greeklish) System learn greeklish. Learn to use your Keyboard shortcuts makes your life easier.Inside time άλλωστε... εμείς είμαστε ...
This entry was posted in Coding. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s