Solved

How to get a session object from a session id

Posted on 2004-10-26
206 Views
Last Modified: 2010-04-01
Hi,

I have two sessions A & B, each set their own variables.   When I'm in a JSP that has session A, I would like to retrieve attributes set in session B.  I have session B session ID.  How do I retrieve session B's object and therefore its attribute values using its session ID?

Thanks.
0
Question by:tlchu
    14 Comments
     
    LVL 27

    Expert Comment

    by:rrz
    You can use the javax.servlet.http.HttpSessionAttributeListener interface.  It can listen for changes in session attributes. You can get the session id from the HttpSessionBindingEvent that occured. You could maintain a table of session ids and  lists of their session attributes. You could hold the data as an application attribute.
    But maybe someone has a better way.           rrz
    0
     
    LVL 13

    Expert Comment

    by:petmagdy
    I think it is better to use Servlet engine application scope for data shared between sessions
    0
     

    Expert Comment

    by:MatthewDunn
    This would be useful for reattaching a user to a preexisting session. [Unfortunately, this behavior has been deemed dangerous. The code below used to work, but has been deprecated as of Servlet API version 2.1. (The method HttpSessionContext.getSession(String sessionid) has been deprecated.)
    There may be a way to make it work under certain servlet engines, but no portable/standard way that I know of.]

    Code sample:
    --------------------------------------------------------------------------------
    // first, store the target HttpSession id, no problems about this cause the id is a Serializable String object
    HttpSession oldSesion=request.getSession(false);
    String id=oldSesion.getId();
    database.storeID(id);

    // next, maybe in another servlet or object
    // recover target ID
    String idOldSesion=database.recoverID();

    // recover target HttpSession
    HttpSession currentSesion=request.getSession(false);
    HttpSessionContext sCtx=currentSesion.getSessionContext();
    HttpSession recoveredOldSesion=sCtx.getSession(idOldSesion);


    --------------------------------------------------------------------------------
    Test conditions:
    BEA Weblogic 4.5.1 (uses httpServlet Specification 2.1 ).
    Windows NT 4.0
    0
     
    LVL 13

    Expert Comment

    by:petmagdy
    MatthewDunn starting from J2EE 1.2 implmentations this will return null:

    HttpSession recoveredOldSesion=sCtx.getSession(idOldSesion);

    recoveredOldSesion will equals to null, the best schema is to use application scope
    0
     

    Author Comment

    by:tlchu
    I found the documentation for HttpSessionContext, but noticed that it is deprecated so I did not use it.

    petmagdy, could you please explain Servlet application scope?

    Is there away to get sessions from a JSP?
    0
     
    LVL 13

    Expert Comment

    by:petmagdy
    ok,

    consider the application scope is a place in memory that is shareed between all sessions

    starting from Session B u know that attribute for example "name" may be used from another session, so u put it into the Application scope from ur JSP like this:

    getServletConfig().getServletContext().setAttribute(sessionBId +"_name", nameValue);

    ( if from servlet use getServletContext().setAttribute(sessionBId +"_name", nameValue) directly )

    from the Session A if u want to retrieve this name value using Session B ID do from jsp:
    nameValue = getServletConfig().getServletContext().getAttribute(sessionBId+"_name");

    ( if from servlet use getServletConfig().getServletContext().getAttribute(sessionBId+"_name"); directly )
    0
     
    LVL 13

    Expert Comment

    by:petmagdy
    sorry little correction when I say "( if from servlet use getServletConfig().getServletContext().getAttribute(sessionBId+"_name"); directly )"

    I mean "( if from servlet use getServletContext().getAttribute(sessionBId+"_name"); directly )"

    Also take care on session expiration u should call:

    getServletContext().removeAttribute(sessionBId +"_name");

    or otherwize after long time period this cause memory problems
    0
     
    LVL 27

    Expert Comment

    by:rrz
    To tlchu, Please describe your requirements in more detail.
    Maybe using listeners isn't necessary. Maybe using session id isn't necessary either.
    Maybe, you could use something like the following two lines in a  jspInit()
    HashMap usersMap = new HashMap();
    application.setAttribute("users",usersMap);
    then in the service part of your page you could create a map for each user(John for example) to hold their variables.
    HashMap johnMap = new HashMap();
    johnMap.put(var1,"value")
    ((HashMap)application.getAttribute("users")).put("John",johnMap);
    You could access the variables with
    (String)((HashMap)((HashMap)application.getAttribute("users").get(johnMap)).get(var1);

    To petmagdy,
    >I think it is better to use Servlet engine application scope for data shared between sessions
    That what I meant when I posted
    >You could hold the data as an application attribute.

    >Also take care on session expiration u should call:
    Good idea, maybe use the  HttpSessionListener interface.
    0
     

    Author Comment

    by:tlchu

    Regarding using getServletContext attributes, this would work it it's the same servlet.  However, the problem is they are different servlets and different session but share some info.
    0
     
    LVL 13

    Expert Comment

    by:petmagdy
    no the servlet context is shared amoung all servlets of a single web application
    0
     

    Author Comment

    by:tlchu

    Thanks for the info.

    The problem I have is that these servlets are in different webapps.  Is there a way to get around this?  Perhaps, they shouldn't be accessing shared info, but that's another story.
    0
     
    LVL 13

    Expert Comment

    by:petmagdy
    tlchu, u have one o the following approches:

    1- Use database instead of application scope

    2- try to refer to ur Application Server or servlet engine documentation to see if can configure that ur different web apps share the Application Scope

    3- If ur Application Server is J2EE 1.3 certified store into JNDI instead of Application Scope

    give me ur application server name and verion to see what to do
    0
     

    Author Comment

    by:tlchu

    I'm using Tomcat version 5.0.25.

    I don't want to use database.  Please advice on JNDI.  Thanks.
    0
     
    LVL 13

    Accepted Solution

    by:
    Please try crossContext in Tomcat configuration to make the servlet context shared between different Web Apps, refer to this linl:

    http://jakarta.apache.org/tomcat/tomcat-5.0-doc/config/defaultcontext.html
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Phishing is at the top of most security top 10 efforts you should be pursuing in 2016 and beyond. If you don't have phishing incorporated into your Security Awareness Program yet, now is the time. Phishers, and the scams they use, are only going to …
    Resolve Outlook connectivity issues after moving mailbox to new Exchange 2016 server
    This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    934 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now