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


Thiago Souza said...

You could serve static content, outside of war, with tomcat 6 too (even with earlier versions). This is not new.

Derrick said...

True, but not within the same context. This is new.

Anonymous said...

This is bullshit, I can do it since tomcat 5!

madth3 said...

I think "outside of war" is not a good way of describing this new feature.

Anonymous said...

Thanks for highlighting the new way to get tomcat 7.0 to serve static files.

Igor Semenko said...
This comment has been removed by the author.
Igor Semenko said...

Here is what worked for me, notice 'path'

< Context path="/myappcontext" aliases="/images=/some/path/images" / >