How can I get a context (application) init parameter from a javabean
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   guido_aulisi
Posted On:   Wednesday, May 2, 2001 07:41 AM

I'm writing a bean which implements a pool of db connections. I need to read the parameters from the web.xml file. I'm using Tomcat. This is the content of web.xml




DBUrl
theURL



DBDriver
theDriver



DBUser
theUser



DBPassword
thePassword




I know how to read these parameters from a servlet or jsp page, but how can I read these ones from a JavaBean?
Thanks

Re: How can I get a context (application) init parameter from a javabean

Posted By:   AlessandroA_Garbagnati  
Posted On:   Wednesday, May 2, 2001 10:09 AM

Guido,

There are two ways to solve this problem. The first one is more complex and it's based on the bean reading those values directly from the file. As you can immagine, this can be a problem (os dependancy, no portability).

The other solution is to have a method in your bean that receives a ServletContext as a parameter and extract the parameters from that object.

The reason is simple: the bean has absolutely no knowledge about that file and the only way to fix this is to provide it the location (so you can read it) or an object that knows it (the context).


I think that you would like to use the bean almost like a singleton that handles your db connection. You can create a servlet that it is loaded at startup (so init() it's executed when the servlet container starts) that has in the init() method the logic for reading the values and pass them to the bean. I think that this is a widely used solution.
About | Sitemap | Contact