0

I have two context files as main-context.xml and sub-context.xml. I would like to import/include sub-context.xml into main-context.xml.

Is it possible to inject a context.xml into another context.xml in Tomcat?

main-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <Valve className="org.apache.catalina.valves.rewrite.RewriteValve"/>              
              
    <Resource name="mainDs" auth="Container"
              type="javax.sql.DataSource"
              driverClassName="oracle.jdbc.OracleDriver"
              url="jdbc:oracle:thin:@localdomain:1521/test"
              username="test_username" password="test_password" 
              maxTotal="20"
              maxIdle="10"
              maxWaitMillis="-1"
              validationQuery="SELECT 1 FROM DUAL"
              validationQueryTimeout="3"
              testOnBorrow="true"
              testWhileIdle="true"
              defaultTransactionIsolation="2" />
    
    <!-- Default set of monitored resources. If one of these changes, the    -->
    <!-- web application will be reloaded.                                   -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->
</Context>

sub-context.xml:

<Resource name="subDs" auth="Container"
              type="javax.sql.DataSource"
              driverClassName="oracle.jdbc.OracleDriver"
              url="jdbc:oracle:thin:@localdomain:1521/testsub"
              username="testsub_username" password="testsub_password" 
              maxTotal="20"
              maxIdle="10"
              maxWaitMillis="-1"
              validationQuery="SELECT 1 FROM DUAL"
              validationQueryTimeout="3"
              testOnBorrow="true"
              testWhileIdle="true"
              defaultTransactionIsolation="2" />
Jeff Scott Brown
  • 26,804
  • 2
  • 30
  • 47
  • 3
    Depending on how the Tomcat XML parser is implemented, you might be able to use an external entity definition. https://stackoverflow.com/q/5121052/18157 – Jim Garrison Feb 22 '22 at 01:59
  • Tomcat **does** support external entities in its configuration files, so Jim's proposal will work. There is also a hierarchy of default `context.xml` files that will be merged with your application's context file (cf. [defining a context](https://tomcat.apache.org/tomcat-9.0-doc/config/context.html#Defining_a_context)). – Piotr P. Karwasz Feb 22 '22 at 07:52

0 Answers0