Hello,

I have a servlet that traverses a hashtable that
contain user logins mapped to their session objects.
Every hour, I want this servlet to run and clear those
user logins whose session objects have not changed in
the last half hour. I wrote the following code, but I
config.getServletContext is returning a null object.
Have I called the run() method correctly? If not, is
there any other way to call this servlet every hour?

********* CODE BEGINS HERE ************
import java.io.*;
import java.lang.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ClearSessions extends HttpServlet
implements Runnable
{
private ServletContext context;
private boolean upAndRunning;

public void init(ServletConfig config)
throws ServletException
{
   context = config.getServletContext();
   upAndRunning = false;
}

public void doGet(HttpServletRequest request,
HttpServletResponse response) throws IOException,
ServletException
{
 if (!upAndRunning)
 {
  upAndRunning = true;
  (new ClearSessions()).run();
 }
}

public void run()
{
Hashtable users = (Hashtable)
context.getAttribute"users");

for (Enumeration e = users.keys();
e.hasMoreElements();)
 {
 String login = (String)e.nextElement();
 HttpSession session = (HttpSession)users.get(login);
 Date date = new Date();

 long gap = date.getTime() -
session.getLastAccessedTime();

 if (gap > (1000*1800)) //Half an hour
  {
  users.remove(login);

  DBA dba = (DBA)session.getValue("dba");
  dba.close();
  dba = null;

  session.invalidate();
  }
 }

 try
 {
  Thread.sleep(30); // Sleep for one hour
 }
 catch(InterruptedException ie)
 {
 }
}
}

********* CODE ENDS HERE ************

The DBA object is a database object that is
instatiated everytime a user logs into the website. I
have written a close() method that closes the
connection object and sets the statement objects to
null.

Thanks and bye,
Arun Jayaprakash.

__________________________________________________
Do You Yahoo!?
Yahoo! Photos - Share your holiday photos online!
http://photos.yahoo.com/

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to