Tomcat 7 context parameter limitation

I am trying to override a parameter in my web.xml application 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

 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 <Parameter name="port" value="100" override="1"/> directly in context.xml in <tomcat-home>/context.xml , then it will work.

Can someone explain what I'm doing wrong?

+4
source share
1 answer

This is because such an application context does not exist with the name context. In other words, there is no web application with a name context deployed in the webapps directory.

Create official Tomcat 7 documentation related to Context Definition :

Individual context elements can be explicitly defined:

  • In a separate file in the / META -INF / context.xml file inside the application files. Optionally (based on the Host copyXML attribute) this can be copied to $ CATALINA_BASE / conf / [enginename] / [hostname] / and renamed to the name of the base application file plus the extension ".xml".

  • In separate files (with the extension ".xml") in the $ CATALINA_BASE / conf / [enginename] / [hostname] / directory. The context path and version will be obtained from the base file name (the file name is less than the .xml extension) . This file will always take precedence over any context.xml file packaged in the META-INF Directory web application.

  • Inside the Host element in the main conf / server.xml.

So for it to work, name your custom file, not context.xml, but your_app_name.xml .
In your case, it will be (if I understand you correctly) myapp.xml .

That should work. I just tested it.

myApp.xml

 <?xml version="1.0" encoding="UTF-8"?> <Context> <Parameter name="port" value="100" override="1"/> </Context> 

PS

And you can get without a path attribute, so don't include it.
From the Apache Tomcat 7 documentation :

This attribute should only be used with a static Context definition in server.xml . In all other circumstances, the path will be inferred from the file names used for the .xml or docBase context file.

Even with a static Context definition in server.xml, this attribute should not be set if either docBase is not under Host appBase or both deployOnStartup and autoDeploy are false . If this rule is not respected, a double deployment may occur.

+6
source

All Articles