Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
JSP Session Counter Using SessionListener 
 

Counter is a device which stores the number of times a particular event or process has occurred.

 

JSP Session Counter Using SessionListener

                          

Counter is a device which stores the number of times a particular event or process has occurred. We can say that in general there is two types of counters, first is up counters which counts upwards and the second one is downward counter which counts from up to down.

To make a program on session counter we are making use of the HttpSessionListener interface.This listener is a part of HttpSession for the HttpSession object.

HttpSessionListener: By using the HttpSessionListener for tracking the number of users who are logged into an application. In HttpSessionListener we can implement a counter every time a session is created and decrement it every time when the session is destroyed.

HttpSessionListener has two methods.

  1. sessionCreated(HttpSessionEvent event)
  2. sessionDestroyed(HttpSessionEvent event)

The class HttpSessionEvent class has only one method: getSession()

The code of the program is given below:

<html>
<head>
<title>Page 1</title>
</head>
<body>
<h1>Session Counter Demo</h1>
<a href="SessionCounterJspPage2.jsp">Click here</a> to visit second page .
</body>
</html>

 

<html>
<head>
<title>Page 1</title>
</head>
<body>
<h1>Session Demo Using HttpSessionListener</h1>
<a href="<%=response.encodeURL
("SessionCounterJspPage2.jsp")%>">Click here</a> to visit page page.
</body>
</html>

 

<html>
<head>
<title>Page 2</title>
</head>
<body>
<h1>Session Counter</h1>
<a href="<%=response.encodeURL
("SessionCounterJspPage1.jsp")%>">Click here</a> to visit first page.
</body>
</html>

 

<html>
<head>
<title>Session Counter</title>
</head>
<body>
<h1>Session Counter</h1>
There are 
<%=Session.SessionCount.getNumberOfSessionsCount()%> 
active sessions in the server.
</body>
</html>

 

package Session;
import javax.servlet.http.*;
public class SessionCount implements HttpSessionListener{
  private static int numberOfSessionsCount = 0;
  public void sessionCreated (HttpSessionEvent evt){
    numberOfSessionsCount++;
  }
  public void sessionDestroyed (HttpSessionEvent evt){
    numberOfSessionsCount--;
  }
  // here is our own method to return the number of current sessions
  public static int getNumberOfSessionsCount(){
    return numberOfSessionsCount;
  }
}

The output of the program is given below:

Download this example.

                          

» View all related tutorials
Related Tags: java c web com ide session data application io module copy help sessions sed output vi value using this id

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

What is the meaning of Session,Session id, Cookies?
how to using ?

Posted by Raja on Thursday, 03.8.07 @ 19:59pm | #11170

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.