[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How to get a session object from a session id

Posted on 2004-10-26
14
Medium Priority
?
211 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
Comment
Question by:tlchu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 2
  • +1
14 Comments
 
LVL 28

Expert Comment

by:rrz
ID: 12417550
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
ID: 12419448
I think it is better to use Servlet engine application scope for data shared between sessions
0
 

Expert Comment

by:MatthewDunn
ID: 12421072
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 13

Expert Comment

by:petmagdy
ID: 12421164
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
ID: 12422162
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
ID: 12422404
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
ID: 12422467
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 28

Expert Comment

by:rrz
ID: 12424222
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
ID: 12425637

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

Author Comment

by:tlchu
ID: 12428007

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
ID: 12428058
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
ID: 12428151

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:
petmagdy earned 1500 total points
ID: 12431304
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

650 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