• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Session counting problem

 
PradeepPillai Pradeep
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following listner to count the active sessions.
---------------------------------------------------------
package com.cs;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class SessionCounter implements HttpSessionListener {

privatestatic int activeSessions =0;

/**
* @see javax.servlet.http.HttpSessionListener#void (javax.servlet.http.HttpSessionEvent)
*/
public void sessionCreated(HttpSessionEvent arg0) {
activeSessions++;
System.out.println(activeSessions + " SESSIONS ARE CREATED");

}
/**
* @see javax.servlet.http.HttpSessionListener#void (javax.servlet.http.HttpSessionEvent)
*/
public void sessionDestroyed(HttpSessionEvent arg0) {
if (activeSessions>0){
activeSessions--;
System.out.println(activeSessions + " SESSIONS ARE DISTROYED");
}
}



public static int getActiveSessions() {
System.out.println("THE NUMBER OF ACTIVE SESSIONS IS : " + activeSessions);
return activeSessions;
}

}
-------------------------------------------------------

I have the following tester class to test the listner.
------------------------------------------------------
package com.cs;

import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
* @version 1.0
* @author
*/
public class TestSessionCounter extends HttpServlet implements Servlet {

/**
* @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//SessionCounter s = new SessionCounter();
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.println("The number of sessions Active is: " + SessionCounter.getActiveSessions());

}

/**
* @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

}

}
------------------------------------------------------

Whenever I open a new session, it counts it. But when I close the browser, it doesn�t decrement the count. Anybody know why? Any help greatly appreciated.
Thanks in advance.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Closing the browser does not expire the session, only logging out or having the session time out will do this. In this case the session will only expire some time after the session expirey time. Be careful not to assume that sessions expire as soon as they reach their expirey time, sometimes they only get closed by a very low level thread some time after they have timed out.
 
PradeepPillai Pradeep
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. One other thing is that if I opened the �TestSessionCounter� servlet in a browser window, the sessioncounter wouldn�t count this at all. I am wondering why!
 
Harpreet Hira
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is one evident problem in your servlet ... you are NOT creating session. You need to create session, as it in automatically created by web container. Putting "req.getSession();" in servlet's doGet() method should be enough to test.

Also, make sure that you have following entry in web.xml
<listener>
<listener-class>com.cs.SessionCounter</listener-class>
</listener>

as the container needs to be told which is the listener class.
 
PradeepPillai Pradeep
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to both of you. I have another servlet called showsession. Whenever I run showsession, the counter in the sessioncounter gets incremented by 2. Why not by one?

My showsession servelet follows:
--------------------------------
package com.cs;

import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import javax.servlet.http.*;
import java.util.*;

public class ShowSession extends HttpServlet implements Servlet {


public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
String heading = "head";
Integer accessCount;
//out.println("test");
HttpSession session = req.getSession(true);
accessCount = (Integer)session.getAttribute("accessCount");
//System.out.println(accessCount);

if (accessCount==null){
accessCount = new Integer(0);
heading = "Welcome newcomer";
}else {
heading = "Welcome Back";
accessCount = new Integer(accessCount.intValue()+1);
}
session.setAttribute("accessCount", accessCount);

out.println("<html><head></head> <body bgcolor='99CC99'>"+
"<h1 align='center'>" + heading + "</h1>" +
"<h2 align='center'>Information on your session </h2>"+
"<TABLE border=\"1\" bgcolor='CCCC99' align='center'>" +
"<TBODY bgcolor='339999'>" +
"<tr align='center' >" +
"<th>Type <th>Value" +
"<TR>" +
"<TD >" +"ID " +"</TD>" + "<TD >" +session.getId()+ "</TD>" +
"</TR>" +
"<TR>" +
"<TD >Creation Time</TD>" +"<TD >" +new Date(session.getCreationTime() ) + "</TD>" +
"</TR>" +
"<TR>" +
"<TD >Last Access Time</TD>" +"<TD >" +new Date(session.getLastAccessedTime() )+ "</TD>" +
"</TR>" +
"<TR>" +
"<TD >Number of Previous accesses</TD>" +"<TD >" +accessCount+ "</TD>" +
"</TR>" +

"</TBODY>" +
"</TABLE>"+

"</body></html>");


}

public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req,resp);

}

}

--------------------------------
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Be careful not to assume that sessions expire as soon as they reach their expirey time, sometimes they only get closed by a very low level thread some time after they have timed out.


For a long time I had been wondering why sessionDestroyed() method isn't invoked even after the specified time has long gone. Now I understand why that ins't happening.

Thanks dave.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We saw this behaviour in OBM WebSphere. When the session expires, nothing happens. If you attempt to use the session, it gets rejected and expires at this point. However if you never try to use the session, it gets cleaned up a considerable time afterwards, I forget the exact numbers. It made us sad.
 
Harpreet Hira
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pradeep, I executed your servlet in my webserver and it seem to increment the counter by 1.
 
PradeepPillai Pradeep
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I am getting in my server console each time I execute ShowSession servlet.


[5/12/06 17:55:17:039 EDT] 5e8772db WebGroup I SRVE0180I: [CSApplicationWeb] [/CSApplicationWeb] [Servlet.LOG]: ShowSession: init
[5/12/06 17:55:17:508 EDT] 5e8772db SystemOut O 1 SESSIONS ARE CREATED
[5/12/06 17:55:17:508 EDT] 5e8772db SystemOut O 2 SESSIONS ARE CREATED
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic