Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tomcat 7 context parameter override

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?

like image 817
Jeff Storey Avatar asked Feb 04 '13 22:02

Jeff Storey


People also ask

What is docBase in Tomcat context?

The docBase attribute is a path to the WAR file or exploded deployment directory. It is relative to the webapps directory, although an absolute path can be used. The path attribute is the one we are most interested in, as it defines the context path of the application.

Does Tomcat use JNDI?

Tomcat provides a JNDI InitialContext implementation instance for each web application running under it, in a manner that is compatible with those provided by a Java Enterprise Edition application server. The Java EE standard provides a standard set of elements in the /WEB-INF/web.


1 Answers

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.

like image 191
informatik01 Avatar answered Nov 15 '22 07:11

informatik01