Overview
Ever wondered how many users are viewing your web site at this moment? well this article seems to answer that. We will learn how to count active users using one session listener class and a JSP page. Once you have read this article, you'll be able to see the number of active sessions on your web site. This is all fun, so keep reading.
What is a Session ?
A session is a sort of temporary unique connection between the server and client ( browser ) by which the server keeps track of that specific user. That unique session can be used by web applications to display this content to one user and that content to the other user.
As part of Servlet 2.3 specification, we can now make use of session creation and destruction events. Our listener object will be called every time a session is created or destroyed by the server. This is very useful. One use of these events is to count active users which we are going to do in this article.
How to count active Sessions ?
Simply increment a private integer variable every time a session is created and decrement it every time a session is destroyed. We'll learn more about it when we create our session listener class.
What is required ?
A Servlet container or application server which supports Servlet 2.3 specification. Examples of such application servers are Tomcat 4.1, Orion 1.6.0, Resin 2.1.4, BEA Web Logic 6.1 etc.
SessionCounter.java
Create a new Java source file and save it as SessionCounter.java in the /WEB-INF/classes/com/stardeveloper/web/listener/ folder of your web application. Now copy and paste the following code in it :
First line is the package statement. Next are two import statements which import the
Then we declare our SessionCounter class and implement
public class SessionCounter implements HttpSessionListener {
Next we declare a class level ( static ) private integer variable which will hold the active session count for our SessionCounter class.
Now we will implement the two methods of
In
We have implemented both the methods of
Now save SessionCounter.java file and compile it.
Sessions.jsp
Now create a new JSP page in any folder you like accessible by your server and give it any name. For example's sake we call it "Sessions.jsp" and save it in / main folder. Copy and paste the following code in it :
Explanation
There are only two important lines in the Sessions.jsp code above, rest is all client-side HTML. First line is the "page" directive containing an "import" attribute with the value of
Next important line comes when we call the
Next we'll learn what we have to do with the /WEB-INF/web.xml file in order to tell the application server that we have this
Web.xml
Edit your /WEB-INF/web.xml file and make sure it contains <listener> and <listener-class> tags as shown below, if there is no /WEB-INF.web.xml file then create it, copy and paste the following text in it :
If you already have a /WEB-INF/web.xml file then simple copy and paste following text in the middle of your web.xml file :
Ever wondered how many users are viewing your web site at this moment? well this article seems to answer that. We will learn how to count active users using one session listener class and a JSP page. Once you have read this article, you'll be able to see the number of active sessions on your web site. This is all fun, so keep reading.
What is a Session ?
A session is a sort of temporary unique connection between the server and client ( browser ) by which the server keeps track of that specific user. That unique session can be used by web applications to display this content to one user and that content to the other user.
As part of Servlet 2.3 specification, we can now make use of session creation and destruction events. Our listener object will be called every time a session is created or destroyed by the server. This is very useful. One use of these events is to count active users which we are going to do in this article.
How to count active Sessions ?
Simply increment a private integer variable every time a session is created and decrement it every time a session is destroyed. We'll learn more about it when we create our session listener class.
What is required ?
A Servlet container or application server which supports Servlet 2.3 specification. Examples of such application servers are Tomcat 4.1, Orion 1.6.0, Resin 2.1.4, BEA Web Logic 6.1 etc.
SessionCounter.java
Create a new Java source file and save it as SessionCounter.java in the /WEB-INF/classes/com/stardeveloper/web/listener/ folder of your web application. Now copy and paste the following code in it :
/************************************************************ SessionCounter.java ************************************************************/ package com.stardeveloper.web.listener; import javax.servlet.http.HttpSessionListener; import javax.servlet.http.HttpSessionEvent; public class SessionCounter implements HttpSessionListener { private static int activeSessions = 0; public void sessionCreated(HttpSessionEvent se) { activeSessions++; } public void sessionDestroyed(HttpSessionEvent se) { if(activeSessions > 0) activeSessions--; } public static int getActiveSessions() { return activeSessions; } }
Explanation
First line is the package statement. Next are two import statements which import the
HttpSessionListener
interface and HttpSessionEvent
class
.package com.stardeveloper.web.listener; import javax.servlet.http.HttpSessionListener; import javax.servlet.http.HttpSessionEvent;
Then we declare our SessionCounter class and implement
HttpSessionListener
interface. HttpSessionListener
interface is present in the javax.servlet.http
package and our class *has to* implement this interface if we want it to be
told of session creation and destruction events. This interface contains
just two simple methods relative to the session creation and destruction events
as we'll see in a moment.public class SessionCounter implements HttpSessionListener {
Next we declare a class level ( static ) private integer variable which will hold the active session count for our SessionCounter class.
private static int activeSessions = 0;
Now we will implement the two methods of
HttpSessionListener
interface. sessionCreated()
method will be called by the server
every time a session is created and sessionDestroyed()
method will
be called every time a session is invalidated or destroyed.In
sessionCreated()
method we increment our internal integer
variable activeSessions. And in sessionDestroyed()
method we
decrement the value of activeSessions.public void sessionCreated(HttpSessionEvent se) { activeSessions++; } public void sessionDestroyed(HttpSessionEvent se) { if(activeSessions > 0) activeSessions--; }
We have implemented both the methods of
HttpSessionListener
interface, now the only thing remains is to create a public static method to be
called by our JSP page to get the activeSessions count.public static int getActiveSessions() { return activeSessions; }
Now save SessionCounter.java file and compile it.
Sessions.jsp
Now create a new JSP page in any folder you like accessible by your server and give it any name. For example's sake we call it "Sessions.jsp" and save it in / main folder. Copy and paste the following code in it :
<%-- Sessions.jsp --%> <%@ page import="com.stardeveloper.web.listener.SessionCounter" %> <html> <head> <title>Active Sessions</title> </head> <body> <p align="center"> Active Sessions : <%= SessionCounter.getActiveSessions() %> </p> </body> </html>
Explanation
There are only two important lines in the Sessions.jsp code above, rest is all client-side HTML. First line is the "page" directive containing an "import" attribute with the value of
com.stardeveloper.web.listener.SessionCounter
class.<%@ page import="com.stardeveloper.web.listener.SessionCounter" %>
Next important line comes when we call the
public static
method
( getActiveSessions()
) of our SessionCounter
class.
It simply displays the number of active sessions in the browser.Active Sessions : <%= SessionCounter.getActiveSessions() %>
Next we'll learn what we have to do with the /WEB-INF/web.xml file in order to tell the application server that we have this
SessionCounter
listener class so kindly call it's session event methods every time a session is
created or destroyed.Web.xml
Edit your /WEB-INF/web.xml file and make sure it contains <listener> and <listener-class> tags as shown below, if there is no /WEB-INF.web.xml file then create it, copy and paste the following text in it :
<!-- Web.xml --> <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/j2ee/dtds/web-app_2.3.dtd"> <web-app> <!-- Listeners --> <listener> <listener-class> com.stardeveloper.web.listener.SessionCounter </listener-class> </listener> </web-app>
If you already have a /WEB-INF/web.xml file then simple copy and paste following text in the middle of your web.xml file :
<!-- Listeners --> <listener> <listener-class> com.stardeveloper.web.listener.SessionCounter </listener-class> </listener>
Now stop and restart your application server and call your Sessions.jsp page. You should be able to see number of active sessions.
In this article we learned what are sessions and how to count active sessions.
We learned that to count active sessions we have to create a simple Java class
and implement
We also learned how to tell the server about our listener class by putting special <listener> and <listener-class> tags in /WEB-INF/web.xml file.
We finally created a JSP page to try out our session listener class and found out that it was working well and we were able to see a count of active users on the site.
HttpSessionListener
interface which is present in the
javax.servlet.http
package. This interface contains just two
methods relative to session created and destroyed events.We also learned how to tell the server about our listener class by putting special <listener> and <listener-class> tags in /WEB-INF/web.xml file.
We finally created a JSP page to try out our session listener class and found out that it was working well and we were able to see a count of active users on the site.
No comments:
Post a Comment