Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
400 views
in Technique[技术] by (71.8m points)

java - Configure Tomcat to use properties file to load DB connection information

What are the accepted practices for creating a Tomcat deployment that reads configuration parameters from a properties file?

It would be nice to be able to deliver a WAR file and specify that the client need only create or edit a properties file in a specific directory. Is this a somewhat regular way of doing things? Is there a better approach than this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

We often distribute webapps by providing a WAR, and a Context XML file, which gets placed into your tomcat/conf/Catalina/localhost directory, and can load the webapp from any path. There is a reference document here. This provides the following advantages:

  • Context parameters can be configured here and read by the webapp
  • DataSources can be defined and configured here
  • The WAR can actually live anywhere on the filesystem, which means that if Tomcat gets upgraded, only this single configuration file needs to be moved to the new Tomcat install, the web application and any other files can stay where they are

If you really want a properties file, you can set a parameter in the context XML file pointing to your properties file, read the parameter in a ServletContextListener and then read in the properties file.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...