This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Servlets and the fly likes Accessing session object from standard class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Accessing session object from standard class" Watch "Accessing session object from standard class" New topic
Author

Accessing session object from standard class

Gezza Hall
Ranch Hand

Joined: Jan 04, 2005
Posts: 33
Hi all.

Can a standalone class access the session somehow? Basically, I want to call a utility class which isn't a servlet. I want this class to return the value of a session attribute.

The utility class is going to be called by Ajax via DWR, so I won't be able to pass in the session object as an argument. So the utility class will look something like this:



Can this be done, or are there any suggestions on an alternative? Ultimately I need my javascript to be able to access a session variable.

Many thanks.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

i Think TagHandler(simple/classic tag) is better option

Hope This Helps
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
It is NOT a good idea to pass around references to anything which is managed by the servlet container, such as request, response, or session if you can possibly avoid it.

You need to redesign your requirements for this "utility" class - getting the value of a session attribute takes very little code in your servlet.

I can't imagine what TagHandler has to do with this.

Bill
sudhir nim
Ranch Hand

Joined: Aug 29, 2007
Posts: 212

You can create a Web Context Holder class that manages thread local objects of request,response,session and a filter which sets this object in Web Context holder than your utility or what ever other classes can obtain the reference to those objects from the holder.

Look at the thread http://www.theserverside.com/patterns/thread.tss?thread_id=47208.


[Servlet tutorial] [Servlet 3.0 Cook Book]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing session object from standard class