Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HttpSessionListener

 
Nigel King
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without seeing your code, we would have no way of helping you with this.
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nigel King, please post your code.
 
Nigel King
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 109
 
Peter Sin
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic