By Jairo


2012-06-24 13:25:55 8 Comments

How do I get the number of active sessions in Xpage. I'm trying to use managed beans but it just returns a weird string. Here's the simple code:

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

public class SessionCounterListener implements HttpSessionListener {

  private static int totalActiveSessions;

  public static int getTotalActiveSessions(){
    return totalActiveSessions;
  }

  public void sessionCreated(HttpSessionEvent arg0) {
    totalActiveSessions++;
    System.out.println("sessionCreated - add one session into counter");
  }

  public void sessionDestroyed(HttpSessionEvent arg0) {
    totalActiveSessions--;
    System.out.println("sessionDestroyed - deduct one session from counter");
  } 
}

I got this from here. But when I call SessionCounterListener.getTotalActiveSessions(), it only returns 0 (regardless if someone is logged in).

If you have the time, you can check out my test database here.

Please help me. Thanks a lot!

1 comments

@Sven Hasselbach 2012-06-25 04:16:49

A session listener must be injected in the application to get working as desired. Otherwise the sessionCreated/sessionDestroyed methods will never be called and 0is returned always. But there are some differences between XPages and "traditional" JSF. Have a look here:

http://www.openntf.org/projects/pmt.nsf/66d9103768cc2fed85256c59006b5433/00a38cdf382f4893862578b80082dd15!OpenDocument

Or have a look inside the XSP Starterkit.

EDIT:

To add a session Listener, do the following:

  1. Switch to java perspective
  2. In the "Code/Java" folder, create a folder "META-INF"
  3. In this META-INF folder, create a folder "services"
  4. In the "services" folder create a file named "com.ibm.xsp.core.events.SessionListener"
  5. In this file, add the full name of your class: package.SessionCounterListener

Now, the session listener should be activated.

@Jairo 2012-06-25 06:28:26

the XSP StarterKit has no documentation. :(

@Sven Hasselbach 2012-06-25 07:21:49

Added a quick description to my reply above.

@Jairo 2012-06-25 07:30:02

Thanks for the edit. I suppose you are using 8.5.3, because of the existence of the Java folder. Unfortunately, I'm using 8.5.2 :(. Can you help me with that. I have the code, but the problem is the services folder.

@Jairo 2012-06-25 08:21:15

I already registered my SessionCounterListener in the faces-config.xml, so I suppose this is not the same in the "services" folder. And I tried to test print by calling a method in the SessionCounterListener and it works. It's just that it doesn't run the sessionCreated nor sessionDestroyed function.

@Sven Hasselbach 2012-06-25 08:48:20

you can not register a sessionListener in faces-config.xml. you have to register in web.xml. You have created a simple bean, nothing else. Thats why it is not working and not running the created/destroyed methods.

@Jairo 2012-06-25 09:31:27

Can you tell me in which directory is the web.xml located? I cannot find it in Package Explorer. :(

@Sven Hasselbach 2012-06-25 10:10:53

You cannot find web.xml, because it is not there. It is an internal file inside the domino core. I just wanted to explain why you cannot register a session listener in a faces-config.xml. I will update the response with a solution for 8.5.2, but I have no time at the moment.

@Jairo 2012-06-25 10:16:46

I really appreciate your help. I'll wait for your response. Thanks a lot :D.

@stwissel 2012-06-26 00:16:07

Jairo there is a deck on the XSP Starter kit on Slideshare. Also the kit is actually a collection of samples that explain the xsp api. That documentation including JavaDoc is on IBM's developerworks and in the XPages extension library book. A META-INF in your sec folder (in webcontent/META-INF/src I presume) should work too - alternative: create services in the META-INF folder directly. One of the two should work

@Jairo 2012-06-26 01:59:11

Hi Stephan! I would like to do the second approach, but I don't find any webcontent/META-INF/src directory and I don't understand what you mean about the "sec" folder. Can you explain it to me further? I'm really sorry. I'm just starting with this advanced XPage development thing. Your explanation would help me a lot! :D. Thanks for the comment :D

@Jairo 2012-06-26 11:16:48

@SvenHasselbach I was able to make it work, but in 8.5.3. I tried to do the same (copying the directories, etc.) to my 8.5.2 database and it still didn't work. I was able to access a function in the Listener class, but the sessionCreated and sessionDestroyed are not being executed.

@stwissel 2012-06-26 14:03:57

Typo. It should read src. Where do you store your Java soure files? In 852 the only save way is to create a src folder under /webcontent/web-inf and add that to the Java sources. Never put stuff in the /Local folder

@stwissel 2012-06-26 14:06:50

You need to make sure your classes end up in the right place. The build path is essential. Wnhen you copy directories the build path is not auto adjusted. Right click om thr nsf and select properties to adjust it

@Jairo 2012-06-27 04:00:38

@stwissel yes, I already did that. This is why I can access a sample function that just returns a string. I am able to instantiate the java class in SSJS. The problem is that the service, i think, is not being registered. That is why the sessionCreated and sessionDestoryed does not automatically run when a user access any xpage of my application (In 8.5.3, something is printed in the server console whenever a user access an xpage, that is why I know its working). Thanks again for the comment! :D

@stwissel 2012-06-27 04:48:31

So where exactly did you put the file com.ibm.xsp.core.events.SessionListener ? It should be either in yourdb.nsf/webcontent/META-INF/src/services (and thus get copied into yourdb.nsf/webcontent/META-INF/classes/services or you can try youdb.nsf/webcontent/META-INF/services (one of the two should work) - and then point that to your fully qualified class name

@Jairo 2012-06-27 14:19:37

@stwissel Not pulling your leg or what, I am really thankful to you for trying to help me. I did try the last options you gave me, but I'm still not successful to execute the sessionCreated & sessionDestroyed functions automatically in Domino 8.5.2 . I know this is to much too ask for, but if you have the time, you can check my test database dropbox.com/s/zs098ne95t4cnl1/test_java.nsf (I included it also in the question). The last thing that I did here is to mimic the directories that I did in the 8.5.3 test database. Again, thanks a lot. I really appreciate it.

@mike_x_ 2014-09-22 12:04:21

it still shows 0 for me on 8.5.3 after trying different places to add a file named com.ibm.xsp.core.events.SessionListener having one line in it with my pckg.classname any ideas?

@Sven Hasselbach 2014-09-23 15:57:24

@mike_x_: You have to implement IBMs SessionListener insted of HttpSessionListener

Related Questions

Sponsored Content

2 Answered Questions

[SOLVED] add KeyListener to JLabel

3 Answered Questions

[SOLVED] Java Inventory Program problems

  • 2011-08-27 20:45:15
  • Colby Plank
  • 1708 View
  • 0 Score
  • 3 Answer
  • Tags:   java

2 Answered Questions

[SOLVED] Clone a single tone object

  • 2013-10-10 06:30:29
  • user2865715
  • 256 View
  • -1 Score
  • 2 Answer
  • Tags:   java cloning

2 Answered Questions

[SOLVED] HttpSessionListenerTest, getting an strange error

2 Answered Questions

[SOLVED] @ViewScoped creating new instance on every postback

1 Answered Questions

3 Answered Questions

[SOLVED] Overriding private methods in (non-)static classes

3 Answered Questions

[SOLVED] XPages - Lotus Domino Java - getDocumentByKey

3 Answered Questions

[SOLVED] JSF Active Sessions counter. How to?

Sponsored Content