[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Need a reference to Session within objects activated from servlet

Posted on 2004-11-28
15
Medium Priority
?
148 Views
Last Modified: 2013-11-24
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 :)
0
Comment
Question by:LinkTree
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 13

Accepted Solution

by:
petmagdy earned 352 total points
ID: 12689406
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
0
 

Author Comment

by:LinkTree
ID: 12689469
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>
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12689706
No because u can't grantee that Each User Session is running in a Separate Thread and that each Thread has a unique name
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 3

Expert Comment

by:drjustin
ID: 12690025
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12690296
>>without passing it from the servlet (I'm working with tomcat)

What's the problem with passing it from the servlet
0
 

Author Comment

by:LinkTree
ID: 12695074
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.
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12695095
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
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 348 total points
ID: 12695136
>>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
0
 

Author Comment

by:LinkTree
ID: 12695178
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???
0
 

Author Comment

by:LinkTree
ID: 12695187
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...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12695220
>>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
0
 

Author Comment

by:LinkTree
ID: 12696352
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?!

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12696433
>>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
0

Featured Post

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.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses
Course of the Month18 days, 3 hours left to enroll

830 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