Search code examples
tomcattomcat8

How can I map multiple contexts inside the save server.xml in Tomcat?


I'm running tomcat 8.5 from eclipse and I have context.xml inside METAINF folder, the content of this file is copied automatically to conf/server.xml on tomcat runtime.

What I want to do id to let tomcat copy another context to the server.xml on runtime. The reason is that I need the second context to have path to images so I can access them from outside the app. (from an angualr app in my case).

I tried to add another context tag to the context.xml, here is the complete context xml:

<Context path="/test" docBase="C:\eclipse\workspace\myproj\test"
        debug="5" reloadable="true" crossContext="true">

<Resource name="jdbc/TestDB" 
              auth="Container"
              type="javax.sql.DataSource" 
              username="root" 
              password="simba"
              driverClassName="com.mysql.jdbc.Driver"
              url="jdbc:mysql://127.0.0.1:3306/my_db?autoReconnect=true"              
              testOnBorrow="true"
              removeAbandoned="true"              
              logAbandoned="true"
              maxWait="60"
              maxActive="10" 
              maxIdle="4"/>              
</Context>

<Context path="/images" docBase="C:\Users\john\Documents\images"
        debug="5" reloadable="true" crossContext="true"> 

</Context>

and I'm getting error that only 1 allowed or more preciously:

The markup in the document following the root element must be well-formed.

I also try to create another file with different name but the content is not copied. Any idea where to add/change the configuration to make it work ?

Thanks.


Solution

  • You cannot place more than one 'Context' element is a context.xml file of Tomcat, it violates the wellformedness constraint; an XML document can only have one document element. What you are trying to do makes the context.xml file have more than one document element. Refer Tomcat 9 Documentation. In the section 'Defining the Context', it provides the places you could define a context (note, it is not advisable to place Context elements in server.xml).