aspose file tools*
The moose likes Servlets and the fly likes how to move db info from servlet into a config.properties file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to move db info from servlet into a config.properties file?" Watch "how to move db info from servlet into a config.properties file?" New topic
Author

how to move db info from servlet into a config.properties file?

Chuan Ren
Ranch Hand

Joined: Aug 04, 2003
Posts: 42
I am using connection pooling for database connection using Java servlet. The database information are in the servlet. Can you please tell me how I could make these info in a properties files (say config.properties) which I could edit externally so that I won't have to recompile my servlet each time I change the database password? (I see some java product has this cool features and the config.properties file is even in xml format.) I just wonder how to do it.
There is an example in c13 of http://www.cookconsulting.com/MasteringJSP/MJspSrc.zip
But I could n't make it work after trying for a few days. Can you please help me? (I am able to test the PropStore, it's working. But not sure how to test the ConnectionManager.java).
Thank you. Thank you.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Here is the way I almost always manage properties:
Put the properties fie in the web-inf/classes directory. This directory is on the Web application classpath, so you can use the web app ClassLoader to find the file in this directory.
Use Class.getResourceAsStream to find your properties file and return an InputStream. Use Properties.load to load the properties.
The only trick is to make sure you use the correct ClassLoader so that it can find your properties file in the web application. I tend to have a wrapper class on the Properties, and this class goes in the web-inf/classes directory too. Now its as easy as calling MyProperties.class.getResourceAsStream to find the file every time.
The reason I only do this 'most of the time' is that properties need to be managed differently if you're using EJBs, EARs or multiple web applications and other complications.
Dave
Anthony Watson
Ranch Hand

Joined: Sep 25, 2003
Posts: 327
The answer is a lot simpler that that last guy said. You simply alter your web.xml file to include either <context-param> for all servlets of the webapp or <init-param> for a single servlet. Simply put all your database specific information in the web.xml file and you won't have to recompile your code when you change it or make some obscure call to an unneccesary properties file. Instead, just use getServletContext.getInitParameter("dbURL");


Anthony W.<br />MCP, SCJP 1.4, SCJD, SCWCD 1.3, SCWCD 1.4, SCBCD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to move db info from servlet into a config.properties file?
 
Similar Threads
Problem with classpath in Batchfile
Factory concept to provide DB transparency
How to connect from JBoss to SQL server ?
Exception java.io.IOException: HTTPS hostname wrong:
Not connecting to database through log4j