tags:

views:

1333

answers:

4

I've context.xml in my web/meta-inf/ folder containing database connection (pool) details. Now i want the database details for my application to be provided by end user in some properties file and context.xml reading the db connection info from the properties file instead of hard coing them in the file directly.

is it possible for tomcat to replace the placeholders from some properties file?

i read about context-manager but not sure where to place that.

Please provide your inputs.

Thanks Abhishek

A: 

Adding the JNDI resources to Context.xml isn't enough. You will need to define their use in WEB-INF/web.xml also. Check this for a step by step.

sal
Ok that is fine and its working in my system but the problem is i want my context.xml to read the database details from a properties file i.e. instead of writing user="sa" i want to write user="${username}" and username value will be defined in a properties file.
@Abhishek use ant or maven to filter it from a properties file. I don't think tomcat has any filtering capability.
sal
A: 

As I understand it, your goal is to have application configuration outside of your .war to allow a system administrator to configure the system.

One way to achieve that is to not place the context.xml in your .war file but to distribute that file alongside with your .war. This file then has to be placed placed in CATALINA_HOME/conf/Catalina/HOSTNAME/APPLICATIONPATH.xml (e.g. CATALINA_HOME/conf/Catalina/localhost/myapp.xml). This way, the database connection information can be edited directly in the external context configuration file without changing the .war file, you won't need placeholders in that scenario. This is not the most comfortable way for the user as he/she has to edit an xml file but it should be feasible for most system administrators...

You can find more information on tomcat context configuration at tomcat.apache.org/tomcat-5.5-doc/config/context.html

A: 

I'm not sure it's possible to load the details from a properties file but you can have the details in the central server.xml file rather than context.xml. Once you've achieved that, you could probably externalize the connection details using a standard XML entity reference.

Instead of putting your database connection details in the context.xml, put them in the server.xml under a section, and then add a ResourceLink element to the context.xml that creates a link between the global data source and your context.

Unfortunately the documentation is fairly weak for what you're trying to achieve. Follow the instructions on the "Resource Definitions" section of this page (about half way down), and pay particular attention to the (tiny) ResourceLink section below it:

http://tomcat.apache.org/tomcat-5.5-doc/config/globalresources.html

Lee
A: 

If I don't have database connection information at build time, I'll just configure a resource reference and tell the admin what name they need to give the connection pool. They can configure everything in the Tomcat admin console and I never have to know about it. This also makes it easier to build a single WAR file and use it in multiple environments since the database configuration details aren't part of the WAR.

See also: Apache Tomcat 6.0 JNDI Resources

If you do know the details at build time and want to bake them into the WAR, try Ant filters.

Isaac Truett