Link to home
Start Free TrialLog in
Avatar of LinkTree
LinkTree

asked on

Need a reference to Session within objects activated from servlet

Hi,
I have a project I'm working on where I need access to user name and some more parameters that are present in the session object - but where I need it I don't have access to the request object (those are sub classes that the servlet use)
I hope I could rewrite all the code but I cant - I need an easy way to get access to the session object without passing it from the servlet (I'm working with tomcat)
Any help / idea / suggestion will be appreciated.

Thanx :)
ASKER CERTIFIED SOLUTION
Avatar of petmagdy
petmagdy
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of LinkTree
LinkTree

ASKER

Is there no way to get the session from the current container of the servlet?
OR get the servlet dig out the data and attach it in a map with the thread id so I will be able to take it out when ever I need (by the thread name)?

The problem is  cant change the functions cause I have lots and lots of them...
Thanx again>
No because u can't grantee that Each User Session is running in a Separate Thread and that each Thread has a unique name
Maybe putting some Filter object in your web flow where you could at the start put session in ThreadLocal map and at the end realese it.
Look at this example here. Although this is not HttpSession object, but the idea is the same as you would like.

 - http://www.hibernate.org/42.html
Avatar of CEHJ
>>without passing it from the servlet (I'm working with tomcat)

What's the problem with passing it from the servlet
Basiclly there is no problem but I'm working in a writen code and I have tone of classes I need to add loggin class to.
I want to add the name of the user that is related to the log output line and if I need to add the session obj pointer pass around it will take tone of time to code and test... I hoped some one might know a short cut for this issue.
in any solution u can imagine u mast pass one extra parameter from the Servlet to ur logging classes, for example from ur Servlet can generate a per User session Id and uses a SingleTone object to store the information u need in ur logging classes in a Map for example, but still in order for u to retrieve from this SingleTone class u need that Id generated by the Servlet
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I like your idea petmaqdy but isnt there any way to identify the thread that taking care of this request?
Isn't the thread that run the servlet will be the same one that will run all the subfunctions in it (I dont start any threads my self)?
There is a posibility that the same thread that run my servlet will accept a new session call and start to run my servlet as well as other session servlet? if it can how can it happed???
CEHJ - the log is a singletone in the system - max a singleton per class or package due to that I can connect it with a single session...
>>CEHJ - the log is a singletone in the system

OK. But you don't need to generate an id - the Session already has one and the Session reference would actually be enough. You could try to map:

key: current thread
value: Session

e.g.

Session currentSession = (Session)map.get(Thread.currentThread());

in the logging singleton but that may not be infallible
CEHJ - can I uderstand from your answer that there is one thread to each current session?
Mean that if in the servlet I do:

map.put(Thread.currentThread(),myData);

and later in some sub/other class but in the same servlet flow i do

myData = map.get(Thread.currentThread()) ;

and at the end of the servlet i do:

map.remove(Thread.currentThread());

I can count on that that untill the end of the servlet no other data (myData) will be added to the map with the same Thread.currentThread() key?!

>>CEHJ - can I uderstand from your answer that there is one thread to each current session?

No you can't rely on that ;-) This is why i said it won't be infallible. But subsequently created threads will replace the current one in the Map for that Session, so at the point of accessing the Map, it could pan out OK.

Do some testing on this