Bob
Bob

Reputation: 11

Tomcat 7 - overriding context.xml

The company I work for hosts a few Java web apps. We have production sites and demo sites on the same box. The demo sites are normally copies of what is seen on production, only the resources point to the demo resources (databases and Realm using javax.sql.DataSource). Due to security concerns, appBase is not defined in the webapp directory and is defined elsewhere on the server.

I am in the process of migrating these application from Tomcat 5 to Tomcat 7. The Tomcat 5 server had all of the context defined within the server.xml. It was very convenient as I only need to copy the files to the directories defined by the host, either production host, demo host or both and the context.xml files would be overridden by the server.xml.

The documentation I've read for Tom7 stresses that this type of arrangement, placing the context in the server.xml, is no longer recommended.

What is the best way to set the server up so that I can override the context.xml that is included in the META-INF directory. I want to be able to copy the web app from test (not the jar file) to the correct directory (production or demo) and the server will use either the production database or demo database respectively.

  1. I defined the context.xml in the conf/Catalina/'host' directory, renaming it to ROOT.xml. I started the server, it loaded the ROOT.xml file and when I tested it I received a '404'.
  2. I changed the ROOT.xml to context.xml.default in the conf/Catalina/'host' directory and restarted the server. This appeared to have no affect on the host; it used the data definitions defined in META-INF/context.xml.
  3. I removed the context.xml.default from the conf/Catalina/'host' directory and restarted the server. As expected, it used the data definitions defined in META-INF/context.xml.

Thanks for your help.

Upvotes: 0

Views: 3992

Answers (1)

Grim
Grim

Reputation: 2030

Usually you have the Project under version-control but you dont like to have the live connection-password/email-smtp-password ... under Version controll.

So you place a context.xml under version-control at webapp/META-INF/context.xml with dummy-password like changeme. Once deployed a webapp with copyXML-Attribute found in the webapp/META-INF/context.xml the webapp/META-INF/context.xml is copied into $TOMCAT_BASE/conf/Catalina/localhost/webapp.xml if not exists.

So you can change the changeme password after deployment in $TOMCAT_BASE/conf/Catalina/localhost/webapp.xml. This way you can be sure theese

  • live-passwords are never under version-control and
  • a deployment does not overwrite the live-passwords.

To use the TOMCAT_BASE/conf/context.xml is only for global configuration (if you read the comment in the file):

<!-- The contents of this file will be loaded for each web application -->

Upvotes: 1

Related Questions