File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes how to move db info from servlet into a file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to move db info from servlet into a file?" Watch "how to move db info from servlet into a file?" New topic

how to move db info from servlet into a 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 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 file is even in xml format.) I just wonder how to do it.
There is an example in c13 of
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
Thank you. Thank you.
David O'Meara

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.
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:
subject: how to move db info from servlet into a file?
It's not a secret anymore!