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. – Nivedita 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 – Nivedita 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,543
  • 4
  • 27
  • 30
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
Nivedita
  • 1,724
  • 2
  • 21
  • 44
  • Thanks for the answers but I already know how to count number of active session. I just want to know that how to get Id's of all the active session. – user2493406 Jul 16 '14 at 02:35
  • saw the link for SO answer in my answer ? – Nivedita Jul 16 '14 at 03:32
  • See this also: http://stackoverflow.com/questions/3271676/how-to-easily-implement-who-is-online-in-grails-or-java-application/3271748#3271748 – Nivedita Jul 16 '14 at 03:34