java left logo
java middle logo
java right logo
 

Home arrow Java EE Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 3947
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Tracking JSP sessions by using listener servlets E-mail
User Rating: / 28
PoorBest 

A session can be terminated in two ways: You force the termination by calling the invalidate method on the session, or the servlet engine times the session out. Depending on the kind of data you store in the session, you might need to perform some kind of cleanup of the session data, like database connection closure. The HttpSessionBindingListener interface defines notification methods that the session object uses to notify objects when they are added to or removed from a session.

The sample below will show the session count go up whenever the object is added to a session, and go down when the object is removed from a session or when the session it belongs to is invalidated.

Here is a listener servlet tracking sessions

package test;

import javax.servlet.http.*;

////////////// counts the number of sessions that are bound to this object

public class SessionListener implements HttpSessionBindingListener
{
    protected int numSessions = 0;


////////////// called every time this object is added to a session:
    public synchronized void valueBound(HttpSessionBindingEvent event) {
        numSessions ++;
    }

////////////// called every time this object is removed from a session:
    public synchronized void valueUnbound(HttpSessionBindingEvent event) {
        numSessions --;
    }

////////////// returns the current number of bound sessions:
    public int getNumSessions() {
        return numSessions;
    }
}


////////////// here is a page using our session listener:

<%@page language="java" import="test.SessionListener" %>

<html>
  <body>

 
////////////// Set up a static SessionListener shared by all 
////////////// instances of this JSP. There is probably only 
////////////// one instance, but just in case the server creates
////////////// multiple instances, this page can handle it. 

<%!
    protected static SessionListener listener = new SessionListener();
%>

<%
  SessionListener l = null;

  // Allow the browser to pass a "removeListener" 
  // parameter to remove a listener from the session.
  if (request.getParameter("removeListener"!= null) {
    session.removeAttribute("listener");

  }

  // Allow the browser to pass a resetSession parameter 
  // to clear out the session.
  else if (request.getParameter("resetSession"!= null) {

    // See whether there is already a session.
    HttpSession oldSession = request.getSession(false);

    // If there is already a session, invalidate it.
          if (oldSession != null) {

      l = (SessionListeneroldSession.getAttribute("listener");
      oldSession.invalidate();

      // Tell the user that the session was reset and show that the
      // bind counts have been updated. Make sure that there was a
      // listener on the old session, too.

      if (l != null) {
%>

  Your current session was reset.
  The listener now has <%=l.getNumSessions()%> active sessions.<p>
<%
    else {
%>
  Your old session didn't have a listener.<p>
<%
    }

    l = null;
    }
  else {
    // See if the listener is already in the session.
           l = (SessionListenersession.getAttribute("listener");

    // If not, add the global copy of the listener to the session.
          if (l == null) {
      // Put the global listener variable into the session.
      session.setAttribute("listener", listener);
      l = listener;
    }
  }
%>

<%
  if (l != null) {
%>

  You have a listener bound to your session.
<%
  else {
%>
  You do not have a listener bound to your session.
<%
  }
%>

  There are currently <%=listener.getNumSessions()%> sessions holding onto 
  the bind listener.

  <p>
  <table>
    <tr>
    <td><a href="BindTest.jsp">Refresh Form</a></td>
    <td><a href="BindTest.jsp?removeListener">Remove Listener</a></td>
    <td><a href="BindTest.jsp?resetSession">Reset Session</a></td>
  </table>
  </body>
</html>


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.