Here is the Code in 3 files.
SessionMonitor.java - the monitor object itself
SessionMonitorListener.java the listener interface
YourServlet.java - example servlet to show how you would use a
SessionMonitor in your Servlet.
//////////////////// SessionMonitor.java ///////////////////////////////
/**
* Title: Project Full of Code Examples<p>
* Description: <p>
* Copyright: Copyright (c) Elishyah Israel<p>
* Company: jSierra Enterprises<p>
* @author Elishyah Israel
* @version 1.0
*/
package examples;
import javax.servlet.http.*;
import javax.servlet.*;
import java.util.*;
public class SessionMonitor
implements Runnable
{
long _lTimeOut = 300, _lInterval = (long)10;
Vector _vSession = new Vector();
Vector _vListener = new Vector();
void addSession(HttpSession ss)
{
synchronized(_vSession)
{
if (!_vSession.contains(ss))
_vSession.addElement(ss);
}
}
public SessionMonitor(long lTimeOut)
{
_lTimeOut = lTimeOut;
}
public void run()
{
while(true)
{
try
{
Thread.sleep(_lInterval);
}
catch(InterruptedException e)
{}
HttpSession ss = null;
Enumeration enum = _vSession.elements();
while(enum.hasMoreElements())
{
ss = (HttpSession) enum.nextElement();
boolean bExpired = (new Date()).getTime() >
(ss.getLastAccessedTime()+ _lTimeOut);
if(bExpired)
{
fireSessionExpiredEvent(ss);
ss.invalidate();
_vSession.removeElement(ss);
}
}//endWhile
}//endWhile true
}//endMethod run
void fireSessionExpiredEvent(HttpSession ss)
{
Enumeration enum = _vListener.elements();
while(enum.hasMoreElements())
{
((SessionMonitorListener)enum.nextElement()).sessionExpired(ss);
}
}
void addSessionMonitorListener(SessionMonitorListener l)
{
_vListener.addElement(l);
}
void removeSessionMonitorListner(SessionMonitorListener l)
{
_vListener.removeElement(l);
}
}
////
////
////
////////////////SessionMonitorListener.java ///////////////////////////
////
////
////
/**
* Title: Project Full of Code Examples<p>
* Description: <p>
* Copyright: Copyright (c) Elishyah Israel<p>
* Company: jSierra Enterprises<p>
* @author Elishyah Israel
* @version 1.0
*/
package examples;
import javax.servlet.http.*;
import javax.servlet.*;
import java.util.*;
public interface SessionMonitorListener
{
public void sessionExpired(HttpSession ss);
}
////
////
////
////////////////YourServlet.java ///////////////////////////
////
////
////
package examples;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class YourServlet extends HttpServlet
{
SessionMonitor _mon = new SessionMonitor(300);//
//Initialize global variables
public void init(ServletConfig config) throws ServletException
{
super.init(config);
_mon.addSessionMonitorListener(new SessionMonitorListener()
{
public void sessionExpired(HttpSession ss)
{
// YOUR CODE HERE TO DO ANY
// "SESSION EXPIRED" PROCESSING
// SUCH AS KILLING THREADS, RELEASING CONNECITONS, ETC.
}
});
Thread thread = new Thread(_mon);
}
//Process the HTTP Get request
public void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
{
PrintWriter out = new PrintWriter (response.getOutputStream());
HttpSession ss = request.getSession(true);
_mon.addSession(ss);
response.setContentType("text/html");
out.println("<html>");
out.println("<head><title>YourServlet</title></head>");
out.println("<body>");
out.println("The servlet has received a GET. This is the reply.");
out.println("</body></html>");
out.close();
}
}
________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets