This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I am using connection pooling for database connection using Javaservlet. 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.
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
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");