Need a reference to Session within objects activated from servlet

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 :)
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.

their is no to do this expect one of the following:
1- the Servlet pass the Request object ot ur sub classes
2- the servlet pass the Session object to ur SubClasses
3- The servlet extract the needed data from the Session and pass then to ur classes

no way else

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
LinkTreeAuthor Commented:
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
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.

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.

>>without passing it from the servlet (I'm working with tomcat)

What's the problem with passing it from the servlet
LinkTreeAuthor Commented:
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
>>and I have tone of classes I need to add loggin class to.

OK - you've got to add logging to a lot of classes - there's no easy way around this, short of using AOP (if you call that easy), so since you've got to do this anyway, just use the same custom logging class that is constructed with a reference to the session
LinkTreeAuthor Commented:
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???
LinkTreeAuthor Commented:
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


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

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


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:


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
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
Java EE

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.