Accessing the servletContext outside of a servlet

This may be a stupid question, but i am trying to create a "global" database connection object for my web app using a servletContextListener and setting it as a context attribute. I have a plain old bean that i want to get the connection from the servletContext but cannot. Inside my bean i have:

Think about it for a minute, how could your plain old bean have an implementation of getServletContext() unless you added one?

Your bean needs the connection (I strongly advise looking into connection pooling rather than using a single global connection), so pass the connection to the bean. It'd be a poor practice to force your bean to be servlet-aware, so you shouldn't be thinking along the lines of passing the servlet context to the bean just so it could pluck out an attribute.

Hi guys... thanks for the quick responses - I understand how the listener works in that it has the event which i extract the context reference too etc. It was in my bean that I was not sure how to then access the context from, and, if it was possible. I looked at an example of setting up the db connection as a resource-ref with pooling and am going that route - thx!!!