This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JSP and the fly likes HttpSessionListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "HttpSessionListener" Watch "HttpSessionListener" New topic
Author

HttpSessionListener

Nigel King
Greenhorn

Joined: Mar 03, 2006
Posts: 29
My JSP includes an HttpSessionListener which counts the number of current sessions and also stores the maximum number of sessions.
So part of the page looks something like
"Currently serving 1 sessions ( Max 2 )"
This is working perfectly when I connect to my site from home. However, I've just connected from work (?!) for the first time and I get
"Currently serving 1 sessions ( Max 1 )"
as soon as I access the page but this drops to
"Currently serving 0 sessions ( Max 0 )"
when I click a link.
Also, I never get to the stage where I have more than one session counted no matter how many browsers I open. It's always
"Currently serving 1 sessions ( Max 1 )"
Clearly the sessions are not being monitored. I have enabled all cookies but still the problem remains.
Does anyone know the cause of this?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Without seeing your code, we would have no way of helping you with this.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Chetan Parekh
Ranch Hand

Joined: Sep 16, 2004
Posts: 3636
Nigel King, please post your code.


My blood is tested +ve for Java.
Nigel King
Greenhorn

Joined: Mar 03, 2006
Posts: 29
Okay, the code for the listener is

public class SessionCounter implements HttpSessionListener
{
private int numberOfSessions = 0;
private int maxSessions = 0;

public void sessionCreated (HttpSessionEvent evt)
{
numberOfSessions++;

if ( numberOfSessions > maxSessions ) {
maxSessions = numberOfSessions;
}

}

public void sessionDestroyed (HttpSessionEvent evt)
{
numberOfSessions--;
}

public int getNumberOfSessions()
{
return numberOfSessions;
}

public int getMaxSessions()
{
return maxSessions;
}

}

As I said, it workds perfectly when I access the site from my laptop at home but not from my work PC.
Would this imply that some browser setting is preventing the creation of the session? Like I said, I've allowed all cookies.

Thanks for looking at this.
Ben Zung
Ranch Hand

Joined: Mar 25, 2004
Posts: 109
Just something I experienced and not sure it is true or false.
Under Windows XP, if I use IE, each time a new instance of IE opened and connect to the server, my server would report a new session created. If I use FireFox, new instance of it would take over an existing session, if one has opened, so to server, no new session created.
Ben Zung
Ranch Hand

Joined: Mar 25, 2004
Posts: 109
This may be handy.
http://www.javaworld.com/javaworld/jw-07-2004/jw-0712-threadsafe.html
Peter Sin
Ranch Hand

Joined: Jan 13, 2005
Posts: 547
Maybe you need to show us the test cases or test procedures step-by-step. I means how you create those multi-sessions.
Nigel King
Greenhorn

Joined: Mar 03, 2006
Posts: 29
Thanks for all these replies.
The sessions are started when a new browser is opened.
Last night, when I went home and logged into my site from there, I found that the SessionListener wasn't working either. However, I'm absolutely sure it was when it was uploaded. And it definitely is from my local box with Tomcat running locally.
So I'm starting to think that the problem doesn't lie in the code, more in some kind of Tomcat setup.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HttpSessionListener
 
Similar Threads
Do classes expire?
how would the sessionListener object be accessed
How many Session Objects ??
getting the IP when the session is created
HttpSessionListener: What were they thinking?