tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Shapira, Yoav" <Yoav.Shap...@mpi.com>
Subject RE: How to access a specific session in a servlet
Date Mon, 23 Jun 2003 13:03:07 GMT

Howdy,

>I am pretty newbie with servlets and I got stock now. How can I access
a
>specific session from Servlet?

You mean a specific session by its ID?  This is possible with a little
work (see below), but not usually a good idea.  If you want the current
user session, just use request.getSession().  From you message, I think
this is really what you want.  If servlet one calls the above, it will
get a session.  If servlet1 puts stuff in the session, and then servlet2
calls request.getSession() it will get the same session (for the same
user).  

Try it out first before trying this:

To get a specific session by ID, you need to keep a map of them.  Use an
HttpSessionListener, e.g.
public class SessionTracker implements HttpSessionListener {
  private static Map sessionsById;

  public void sessionCreated(HttpSessionEvent hse) {
    if(sessionsById == null) {
      sessionsById = Collections.synchronizedMap(new HashMap());
    }
   
    sessionsById.put(hse.getSession().getId(), hse.getSession());
  }

  public void sessionDestroyed(HttpSessionEvent hse) {
    sessionsById.remove(hse.getSession().getId());
  }

  public static HttpSession getSession(String sessionId) {
    return (HttpSession)sessionById.get(sessionId);
  }
}

Then you servlets can do SessionTracker.getSession(sessionId) to get a
specific session.  But like I said, I don't think you need the above.

Yoav Shapira





This e-mail, including any attachments, is a confidential business communication, and may
contain information that is confidential, proprietary and/or privileged.  This e-mail is intended
only for the individual(s) to whom it is addressed, and may not be saved, copied, printed,
disclosed or used by anyone else.  If you are not the(an) intended recipient, please immediately
delete this e-mail from your computer system and notify the sender.  Thank you.


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Mime
View raw message