You are viewing a plain text version of this content. The canonical link for it is here.
Posted to general@jakarta.apache.org by Franklin Russell <fr...@yahoo.com> on 2000/02/07 15:02:48 UTC

tomcat sessions

I am running tomcat 3.0 and NT 4 SP6.  I start up multiple "sessions"
yet my servlet never sees the other sessions.  I get the sessionContext
and try to iterate through the session ids but the Enumeration I get is
empty.  Why can't I see the other sessions?  Any help with this would
be greatly appreciated.  Below is a code snippet of what I'm trying to
do.

Franklin Russell

   // Look for any other sessions and get
    // a pointer to the group object so everyone is 
    // referencing the same object.
    Group group = null;
    HttpSession session = req.getSession( true );
    HttpSessionContext context = session.getSessionContext();
    Enumeration ids = context.getIds();
    while ( ids.hasMoreElements() )
    {
        HttpSession other = context.getSession(
(String)ids.nextElement() );
        group = (Group)other.getValue( "expert.group" );
        if ( group != null )
        {
            System.out.println( "Getting group from another session!"
);
            break;
        }
    }
    if ( group == null )
    {
        System.out.println( "Creating or restoring group" );
        group = new Group();
        try
        {
            group.restore();    
        }
        catch( Exception e ) 
        {
            session.invalidate();
            res.sendRedirect( "/mywebapp/errorpage.jsp" );
        }
    }
__________________________________________________
Do You Yahoo!?
Talk to your friends online with Yahoo! Messenger.
http://im.yahoo.com

Re: tomcat sessions

Posted by "Craig R. McClanahan" <cm...@mytownnet.com>.
Franklin Russell wrote:

> I am running tomcat 3.0 and NT 4 SP6.  I start up multiple "sessions"
> yet my servlet never sees the other sessions.  I get the sessionContext
> and try to iterate through the session ids but the Enumeration I get is
> empty.  Why can't I see the other sessions?  Any help with this would
> be greatly appreciated.  Below is a code snippet of what I'm trying to
> do.
>

HttpSessionContext was deprecated back in the 2.1 version of the API, and
HttpSession.getContext() is required to return an empty enumeration.
Therefore, Tomcat's behavior in this regard is correct.  There is no
mechanism through the current servlet API for a servlet to access any
session other than its own.  If you want this, you'll need to arrange
somehow to register the sessions in your own collection.

>
> Franklin Russell
>

Craig McClanahan



Re: tomcat sessions

Posted by Fernando Salazar <fe...@metatel.com>.
I suggest that you not use sessions for this sort of thing.  The session 
contexts are limited by classloader boundaries, among other things.  In 
JServ, for example, you get a separate context for each zone -- I expect 
Tomcat has something similar.

I suggest you need something like a DB connection pool, but instead of 
returning JDBC connections, it would return your "Group" objects.  Your 
code would end up looking like:

         GroupMgr        gMgr  = GroupManager.getInstance();             // 
a static method to get instance of the group-objects manager
         Group           group = gMgr.getGroup( "expert.group" );        // 
return the instance of the specific group object -
                                                                         // 
create a new one if needed.

 From there, you could stash the Group object in the session, if you wanted.

- Fernando

At 06:02 AM 2/7/00 -0800, you wrote:
>I am running tomcat 3.0 and NT 4 SP6.  I start up multiple "sessions"
>yet my servlet never sees the other sessions.  I get the sessionContext
>and try to iterate through the session ids but the Enumeration I get is
>empty.  Why can't I see the other sessions?  Any help with this would
>be greatly appreciated.  Below is a code snippet of what I'm trying to
>do.
>
>Franklin Russell
>
>    // Look for any other sessions and get
>     // a pointer to the group object so everyone is
>     // referencing the same object.
>     Group group = null;
>     HttpSession session = req.getSession( true );
>     HttpSessionContext context = session.getSessionContext();
>     Enumeration ids = context.getIds();
>     while ( ids.hasMoreElements() )
>     {
>         HttpSession other = context.getSession(
>(String)ids.nextElement() );
>         group = (Group)other.getValue( "expert.group" );
>         if ( group != null )
>         {
>             System.out.println( "Getting group from another session!"
>);
>             break;
>         }
>     }
>     if ( group == null )
>     {
>         System.out.println( "Creating or restoring group" );
>         group = new Group();
>         try
>         {
>             group.restore();
>         }
>         catch( Exception e )
>         {
>             session.invalidate();
>             res.sendRedirect( "/mywebapp/errorpage.jsp" );
>         }
>     }
>__________________________________________________
>Do You Yahoo!?
>Talk to your friends online with Yahoo! Messenger.
>http://im.yahoo.com
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: general-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: general-help@jakarta.apache.org


=======================================
Fernando Salazar <fe...@metatel.com>
w -781-392-2514