Serving static content from linux/tomcat7

Sometimes it makes more sense to serve static content like images to your website from a folder on the disk on your server rather than packaging it into a war file. To serve static content from tomcat7, you don’t need to write any java code or create a war file. You can simply edit the $TOMCAT_HOME/conf/server.xml and add <Context path=”/images” docBase=”/home/ubuntu/images” /> tag to the <Host> tag present in the file. path refers to the url path relative to the host/context and docBase refers to the location on disk. So if you have a url like /images/1.jpg, having the file /home/ubuntu/images/1.jpg will render the image.

If you have a war file and are creating a custom servlet, then you will also need to map the url pattern /images/* to the default servlet in web.xml file.

Additionally, the folder on the disk should have the right permissions to allow read access by tomcat.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s