File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes help with session listener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "help with session listener" Watch "help with session listener" New topic

help with session listener

Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
Please excuse the length of this post. I'm getting pretty frustrated, and so I want to give as much info as might be necessary.

I'm trying to add a session listener to the "mainApp" web application of a production website that currently has a little over 2000 concurrent users at peak time, and this site is powered by Tomcat 4 on Windows. However, my session listener doesn't seem to be listening. At the top of my web.xml (before any servlet elements, and I have nothing like a filter or anything else before it), I have this:

<listener> <listener-class>mainApp.utility.OurSessionListener</listener-class>

Then in JBuilder, I've created this listener class:

package mainApp.utility;

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

import mainApp.beans.*;

public class OurSessionListener implements HttpSessionListener {

public void sessionCreated(HttpSessionEvent se) {
HttpSession session = se.getSession();
UserAcctBean user = (UserAcctBean)
String loginID = user.getLoginID();
System.out.println("Added session: " + session.getId() + " for user
" + user.getLoginID());
SystemControl.addActiveUser(session.getId(), user.getLoginID() );

public void sessionDestroyed(HttpSessionEvent se) {
HttpSession session = se.getSession();
System.out.println("Removed session: " + session.getId());

My SystemControl's static HashMap doesn't seem to be getting updated, and the above methods aren't even getting called because nothing is going to stdout when I log in as different users.

One of the main things I'm not sure about is where this SessionListener class needs to be. Part of the confusion stems from the fact that the original developer set it up so that when JBuilder builds the app it puts all class files in all packages into a single JAR file that we stick into Tomcat\common\lib (whereas all the JSPs go into the Tomcat\webapps\mainApp directory). I've discovered that servlets created in JBuilder do get put into the JAR file, but they're not accessible there and so I have to put them into Tomcat's common\classes directory (and mimic the directory structure implied by whatever package they're in).

I wasn't sure if the same needed to be done with this session listener class, so I also copied its class file to the same place even though it's in the JAR file as well. That doesn't seem to help.

I have the session listener declared in the global web.xml in Tomcat\conf, but then I wondered if it needs to be declared in the application's web.xml located under its WEB-INF directory, and so I declared it in there, and also created the directory webapps\mainApp\WEB-INF\classes\mainApp\utility and copied the class file to there, but this still doesn't seem to help when I restart Tomcat!

When a user logs in, their account info gets loaded and they go to a welcome page that accesses some session beans, so I assume that by that point a session has been created. However, when I check in Tomcat's logs directory, nothing is going to stdout, so it's like sessionCreated isn't being called in my listener class! Any ideas why? Thanks...

Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
In case anyone is searching for the answer to this, check this out:

I agree. Here's the link:
subject: help with session listener
It's not a secret anymore!