I am trying to override a parameter in my application's web.xml file by creating a context.xml file in <tomcatHome>/conf/Catalina/localhost
The context.xml file looks like
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/myapp">
<Parameter name="port" value="100" override="1"/>
</Context>
but I get an error saying
java.lang.IllegalArgumentException: Document base <path-to-tomcat> apache-tomcat-7.0.35/webapps/context does not exist or is not a readable directory
If I put the <Parameter name="port" value="100" override="1"/>
directly in the context.xml
in <tomcat-home>/context.xml
then it works.
Can someon explain what I am doing incorrectly?
It is because there is no such application context with the name context. In other words there is no web app with the name context deployed to the webapps directory.
Form the official Tomcat 7 documentation related to Defining a context:
Individual Context elements may be explicitly defined:
In an individual file at /META-INF/context.xml inside the application files. Optionally (based on the Host's copyXML attribute)
this may be copied to $CATALINA_BASE/conf/[enginename]/[hostname]/ and
renamed to application's base file name plus a ".xml" extension.
In individual files (with a ".xml" extension) in the $CATALINA_BASE/conf/[enginename]/[hostname]/ directory. The context
path and version will be derived from the base name of the file (the
file name less the .xml extension). This file will always take
precedence over any context.xml file packaged in the web application's
META-INF directory.
Inside a Host element in the main conf/server.xml.
So to make it work, name your custom file not context.xml, but your_app_name.xml.
In your case it will be (if I understood you correctly) myapp.xml.
This should work. I have just tested it.
myapp.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Parameter name="port" value="100" override="1"/>
</Context>
P.S.
And you can get without path attribute, so don't include it.
From the Apache Tomcat 7 documentation:
This attribute must only be used when statically defining a Context in
server.xml. In all other circumstances, the path will be inferred from
the filenames used for either the .xml context file or the docBase.
Even when statically defining a Context in server.xml, this attribute
must not be set unless either the docBase is not located under the
Host's appBase or both deployOnStartup and autoDeploy are false. If
this rule is not followed, double deployment is likely to result.