tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jim Cheesman <jch...@msl.es>
Subject Re: AW: Sessions not working right
Date Tue, 27 Mar 2001 09:37:15 GMT
At 10:26 AM 3/27/2001, you wrote:
>During testing (tracked the session id's) i just had the same problem, but
>what can be done about that? Our planned application server is a windows
>2000 server with IIS.
(Not really Tomcat related, but...)

Although I'm not using IIS, I had a similar problem with maintaining 
session information in the way I needed - I need to share information 
within a transaction, which is a function of (in this case) sport, venue 
and transaction number.
For example:
Session Sport   Venue   T#
1               BV      BV1     1
2               BV      BV1     2
3               AT      AT1     1
4               BV      BV2     1
etc.

I'm using the following code to do so:


In the servlet:

String key = sport + venue;
HttpSession session = SessionHandler.getSession(key, 
cgiHandler.getValue("TRN_ID"));
if (session == null)
{
         session = request.getSession();
         SessionHandler.addSession(key, cgiHandler.getValue("TRN_ID"), 
session);
}
session.setAttribute("gridHandler", new GridHandler());

The SessionHandler stores the sessions using the sport and venue as the 
key, so that for a new transaction number the old session is overwritten:

/*
  * Use at your own risk!
  */
package sinfo.jsp.utils;

import javax.servlet.http.*;
import java.util.HashMap;

/**
  *  A Singleton class used to maintain Sessions for a given transaction 
(defined
  *  by transaction number, sport and venue). The Sessions are stored in
  *  SessionHolders, which control the transaction number.
  *
  *@author     Jim Cheesman
  *@created    March 21, 2001
  */
public class SessionHandler
{
         private final static SessionHandler me = new SessionHandler();
         private static HashMap sessions;


         /**
          *  Constructor for the SessionHandler object
          */
         private SessionHandler()
         {
                 sessions = new HashMap();
         }


         /**
          *  Gets the Session attribute of the SessionHandler class
          *
          *@param  key          The key (sport + venue)
          *@param  transaction  The transaction id number, converted to 
String (from pid
          *      query string)
          *@return              The Session, null if this is the first 
request for this
          *      session for the given transaction
          */
         public static synchronized HttpSession getSession(String key, 
String transaction)
         {
                 SessionHolder sh = (SessionHolder) sessions.get(key);
                 if ((sh == null) || 
(!sh.getTransaction().equals(transaction)))
                 {
                         return null;
                 }
                 return sh.getSession();
         }


         /**
          *  Adds a new Session for the given sport and venue. (Note that the
          *  transaction ID is not part of the key, as we are not interested in
          *  maintaining old sessions.
          *
          *@param  key          The key (sport + venue)
          *@param  transaction  The transaction id
          *@param  session      The session
          */
         public static synchronized void addSession(String key, String 
transaction, HttpSession session)
         {
                 SessionHolder sh = new SessionHolder(transaction, session);
                 sessions.put(key, sh);
         }
}

/**
  *  Convenience class that links a Session to an id. (They could not be stored
  *  in the HashMap SessionHandler.sessions as the key must include the ID.)
  *
  *@author     Jim Cheesman
  *@created    March 21, 2001
  */
class SessionHolder
{


         private HttpSession session;
         private String transaction;


         /**
          *  Constructor
          *
          *@param  transaction  The transaction ID as String
          *@param  session      The session associated
          */
         SessionHolder(String transaction, HttpSession session)
         {
                 this.transaction = transaction;
                 this.session = session;
         }


         /**
          *  Sets the Session
          *
          *@param  session  The new Session
          */
         void setSession(HttpSession session)
         {
                 this.session = session;
         }


         /**
          *  Sets the transaction
          *
          *@param  transaction  The new transaction id
          */
         void setTransaction(String transaction)
         {
                 this.transaction = transaction;
         }


         /**
          *  Gets the Session
          *
          *@return    The Session
          */
         HttpSession getSession()
         {
                 return session;
         }


         /**
          *  Gets the transaction number
          *
          *@return    The transaction id
          */
         String getTransaction()
         {
                 return transaction;
         }
}



HTH,
Jim





--

                           *   Jim Cheesman   *
             Trabajo: jchees@msl.es - (34)(91) 724 9200 x 2360
               Personal: mogrim@arrakis.es (34) 606 770 244
              Practice safe eating -- always use condiments.














Mime
View raw message