Article updated on

Tomcat How to Set a folder in our app outside the WAR deployment

You may need to do this for a number of reasons.

  1. You don't want a folder to be removed and created after every deployment with Tomcat.
  2. You want to make your WAR file as lightweight as possible.
  3. You need to reference an external folder with static content in your application.

To do this you need to add a context feature in the server.xml file called docBase, ex.

       <Context docBase="/path/to/my/files" path="/MY-APP/files"/>

 

You can find the server.xml file in the tomcat-installation/conf directory or if we are using and IDE such us eclipse in our Servers configuration folder.

Configuration Example changing the server.xml in an eclipse IDE. The application is named MY-APP

       <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
        <Valve className="org.apache.catalina.valves.AccessLogValve" 
directory="logs" pattern="%h %l %u %t &quot;%r&quot; %s %b" prefix="localhost_access_log." 
suffix=".txt"/>
        
        <Context docBase="/home/ubuntu/content/" path="/MY-APP/img"/>
        <!-- all the files in folder are linked to MY-APP/img  -->
        <Context docBase="MY-APP" path="/MY-APP" reloadable="true" source="org.eclipse.jst.jee.server:MY-APP"/>
      </Host>

 

In this example, because my server is active on port 8080  I could watch my image stored on path /home/ubuntu/content/myimage.gif from my web browser typing the url http://localhost:8080/MY-APP/img/myimage.gif

* You must restart tomcat to make this change take effect.

* Modify example accordingly.