0

I have made an web application in jsp. In this web application one requirement has come to know the number of sessions presently active, id of all the active session and session variable i.e username stored in each session by setAttribute(.., ..) method.

Please Help.

user2493406
  • 11
  • 1
  • 4
  • 1
    If you just want the no. of sessions, you need to have a static field in HttpSessionListener then take a variable and increment it in sessionCreated and decrement in sessionDestroyed. – user63762453 Jul 15 '14 at 17:09
  • 1
    Since Tomcat 7 or 8, Tomcat has the ability for the admin to view this in either the manager or admin app, I don't remember which. So you don't need to make your own app for this. – developerwjk Jul 15 '14 at 17:10
  • posted code for the same – user63762453 Jul 15 '14 at 17:18

2 Answers2

0

First create a Listerner which implements HttpSessionListener implement something like this

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

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

  public static int getActiveSessions() {
    return counter;
  }

In the web.xml register the Listener

  <listener>
    <listener-class>packageName.MySessionCounterListerner</listener-class>
  </listener>

Finally in the jsp display the count

<%@ page import="packageName.MySessionCounterListerner"%>
Active Sessions : <%= MySessionCounterListerner.getActiveSessions() %>
SparkOn
  • 8,806
  • 4
  • 29
  • 34
0

If you just want the no. of sessions, you need to have a static field in HttpSessionListener then take a variable and increment it in sessionCreated and decrement in sessionDestroyed.

Here's a code from xyzws.com

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;
 }

}

in web.xml:

<!-- Listeners -->
<listener>
 <listener-class>com.xyzws.web.utils.MySessionCounter</listener-class>
</listener>

To display the same:

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

Also read this answer on SO.

EDIT:An alternative could be to use ArrayList for storing userIds, Firstly you need to set the userId of the user in a session variable whenever a user logs in(You can use different approaches for this).

public class getSessions implements HttpSessionListener 
{
    ArrayList al=new ArrayList();

    public void sessionCreated(HttpSessionEvent se) 
    {
      al.add(uid);
    }
    public static void getActiveSessions()
    {
        Iterator itr=al.addIterator();
        while(itr.hasNext())
        {
                System.out.println(itr.next());//or whatever you want to do
        }


    }

}
Community
  • 1
  • 1
user63762453
  • 1,734
  • 2
  • 22
  • 44