Wednesday, February 20, 2008

You can get the Count of active sessions using Servelets

A session is just like a temporary unique
connection between the client (browser) and
server which helps the server to keep track of
the specific user. Session creation and
destruction events can help to count the number
of active session. We can create listener object
that will be called every time a session is created
or destroyed by the server.
In this section, SessionCounter is a listener
class which will be registered in the web.xml
file. Create SessionCounter class and implement
HttpSessionListener interface. Implement its two
methods sessionCreated() and
sessionDestroyed() passing HttpSessionEvent
as an argument. In the first method
sessionCreated(), increase the value of the
variable responsible for counting the number
of active sessions because one new session has
been created and in the second method
sessionDestroyed () decrease the value of the
same by one because one session has been
ended. Save this file to the WEB-INF/classes
folder of Tomcat server.
SessionCounter.java
import
javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionEvent;
public class SessionCounter implements
HttpSessionListener {
private static int activeSessions = 0;
private static int destroyedSessions = 0;
private static String aSID = null;
private static String iaSID = null;
public void sessionCreated(HttpSessionEvent
se) {
aSID = se.getSession().getId();
activeSessions++;
}
public void
sessionDestroyed(HttpSessionEvent se) {
iaSID = se.getSession().getId();

if(activeSessions > 0){
activeSessions—;
destroyedSessions++;
}
}
public static int getActiveSessions() {
return activeSessions;
}
public static int getInactiveSessions() {
return destroyedSessions;
}
public static String getActiveSessionID() {
return aSID;
}
public static String getInactiveSessionID() {
return iaSID;
}
}
Now create a servlet that will use above listener’s
variables to show the current status of session.
ServletSessionCounter.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class ServletSessionCounter extends
HttpServlet{
public void doGet(HttpServletRequest
request, HttpServletResponse response)
throws ServletException, IOException{
response.setContentType(“text/html”);
PrintWriter pw = response.getWriter();
HttpSession session=
request.getSession();
pw.println(“No. of active sessions :” +
SessionCounter.getActiveSessions()+”
”);
pw.println(“No. of inactive sessions :” +
SessionCounter.getInactiveSessions()+”
”);
pw.println(“Last Activated Session ID :”
+
SessionCounter.getActiveSessionID()+”
”);
pw.println(“Last Inactivated Session ID :”
+
SessionCounter.getInactiveSessionID()+”
”);
session.setMaxInactiveInterval(2);
}
}


Now register the listener in WEB-INF/web.xml
file which makes aware the server about the
SessionListener class that results in calling its
event methods whenever a session is created
and destroyed. Just add the following lines to
the web.xml file.
web.xml:
…………………
…………………

SessionCounterlistener-class>

…………………
…………………
Now restart the server and call the servlet many
times in different browser windows.

No comments: