Obtain object from session in normal class
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   m_b
Posted On:   Wednesday, February 14, 2007 02:47 PM

Hi, I don't know if thsi is the correct forum so I'll give it a try. I'm currently developing a n-tier webapp. At initialisation I would like to initialize a ConnectionPool object I made and put it into a session object at context initialization in a ServletContextListener class. The ServletContextListener class is initialised directly at the startup of Tomcat or other JSP engine. I would like to do this so when I need the connectionpool object I don't have to create it again, but instead get it from the session. Has anyone done this already or is there a better, cleaner way to access such an object? Hope someone can help. Thanks. Michiel.    More>>

Hi,

I don't know if thsi is the correct forum so I'll give it a try.
I'm currently developing a n-tier webapp.
At initialisation I would like to initialize a ConnectionPool object I made and put it into a session object at context initialization in a ServletContextListener class. The ServletContextListener class is initialised directly at the startup of Tomcat or other JSP engine.
I would like to do this so when I need the connectionpool object I don't have to create it again, but instead get it from the session.
Has anyone done this already or is there a better, cleaner way to access such an object?

Hope someone can help.

Thanks.

Michiel.

   <<Less

Re: Obtain object from session in normal class

Posted By:   Robert_Lybarger  
Posted On:   Wednesday, February 14, 2007 07:30 PM

Putting something like that in a contextlistener (for startup) is pretty much the right way to do it. You wind up putting the object(s) in the application scope (not the session scope), so your servlet/jsp pages can get to them. If you have a user-defined helper class (like a plain old bean), you might need to pass in a reference to the object from the servlet/jsp page. I suppose you could also play with having a static reference in some helper class (and a static getter method) that the contextlistener itself initializes -- then you wouldn't need to pass anything around later. I'm not sure of the design impact of this versus whatever you're going to be designing/working with. Still, the techniques of creating and accessing a static/singleton don't change just b/c a j2ee container has provided the 'main' entry point.
About | Sitemap | Contact