Which Tomcat 5 context file takes precedence?

jeph perro picture jeph perro · Nov 26, 2008 · Viewed 10.3k times · Source

Tomcat documentation says:

The locations for Context Descriptors are;

$CATALINA_HOME/conf/[enginename]/[hostname]/context.xml
$CATALINA_HOME/webapps/[webappname]/META-INF/context.xml

On my server, I have at least 3 files floating around:

1 ...tomcat/conf/context.xml
2 ...tomcat/Catalina/localhost/myapp.xml
3 ...tomcat/webapps/myapp/META-INF/context.xml

What is the order of precedence?

Answer

netjeff picture netjeff · Nov 27, 2008

For the files you listed, the simple answer assuming you are using all the defaults, the order is (note the conf/Catalina/localhost):

...tomcat/conf/context.xml
...tomcat/conf/Catalina/localhost/myapp.xml
...tomcat/webapps/myapp/META-INF/context.xml

I'm basing this (and the following discussion) on the Tomcat 5.5 official documentation for the Context Container.

So if that's the simple answer, whats the complete answer?

Tomcat 5.5. will look in a couple of other places for <Context> elements beyond those you've listed (see the official docs).

The META-INF/context.xml will never be opened if Tomcat finds a Catalina/localhost/myapp.xml. So if you actually have all the files above, its more correct to say the the META-INF/context.xml is irrelevant, not that it's the lowest precedence.

If you say <Context override="true" ...> in your Catalina/localhost/myapp.xml that will make it the highest precedence, regardless of conf/context.xml. Same thing in your META-INF\context.xml, as long as you don't have a Catalina/localhost/myapp.xml (see previous paragraph).

Also, the /Catalina/localhost/ portion of the path in the files above actually comes out of the "default" conf/server.xml and matches the <Engine name="Catalina" defaultHost="localhost">. If your server.xml uses different values for name and defaultHost in the <Engine>, that's the dir structure where Tomcat will look.

Finally, for the ...tomcat\ portion of the files you listed, Tomcat uses the dir from the $CATALINA_BASE environment variable. If that's not set, then it uses the dir from the $CATALINA_HOME environment variable, which is the directory of the Tomcat installation. I like to set and use $CATALINA_BASE so that I don't "pollute" my Tomcat installation.