How to get a session object from a session id

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.
tlchuAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rrzCommented:
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
petmagdyCommented:
I think it is better to use Servlet engine application scope for data shared between sessions
0
MatthewDunnCommented:
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
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

petmagdyCommented:
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
tlchuAuthor Commented:
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
petmagdyCommented:
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
petmagdyCommented:
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
rrzCommented:
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
tlchuAuthor Commented:

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
petmagdyCommented:
no the servlet context is shared amoung all servlets of a single web application
0
tlchuAuthor Commented:

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
petmagdyCommented:
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
tlchuAuthor Commented:

I'm using Tomcat version 5.0.25.

I don't want to use database.  Please advice on JNDI.  Thanks.
0
petmagdyCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.