File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Servlets
Bookmark "Accessing session object from standard class" Watch "Accessing session object from standard class" New topic

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

Joined: Mar 22, 2000
Posts: 13035
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.

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

[Servlet tutorial] [Servlet 3.0 Cook Book]
I agree. Here's the link:
subject: Accessing session object from standard class
It's not a secret anymore!