Saturday, July 10, 2010

tomcat 7.0 - serve content from files outside of war

If your webapp needs to serve static content (large video files or images) you would want to serve them directly from a file system folder instead of bundling it with the war file. This is now possible to do with Tomcat, in the recently released 7.0 version a new feature (aliases) to support webapps to refer to content outside of the war file has been added.

Configuring this is straight forward. You will need to create an application specific content.xml file. This can either be placed inside the webapp (META-INF/context.xml) or setup under tomcat (conf/Catalina/localhost/YourApp.xml).

Add the aliases attribute to the Context element

Now all requests starting with /content will be server directly by local by looking for the files under /var/local/app/content. You can add multiple aliases separated by commas (not sure why they choose attribute instead of nested element for this configuration)

This will help make the war files leaner, as well as enable putting the static files on shared storage to ease deployment.

More details on this attribute can be found in the context configuration reference