Tomcat Images Directory

A short rundown on adding external directories to your Apache Tomcat webapp path.

I started with a simple problem and it turns out there’s a simple solution.

My problem:
In the current application I’m coding I display user data (like many apps).  Included with the user data, the page also shows the user’s photo.  The problem was that the photos were stored in a directory external to Tomcat (D:\photos) and I need to get my webapp access to that directory and have those photos in Tomcat’s path.

My Solution:
To solve this I needed to add what Apache calls a context path which can be done by adding an xml file.

Here’s a bit of given information so this is easier to follow.

  • I’m using Tomcat 5.5
  • The name of my webapp is called MyProject which is located at $CATALINA_HOME/webapps/MyProject
  • The directory I’m trying to add to Tomcat’s path is “D:\photos
  • I want my photos to be accessible in the path $CATALINA_HOME/webapps/MyProject/photos (or http://localhost:8080/MyProject/photos).

Creating the context file:

  • In $CATALINA_HOME/conf/[enginename]/[hostname]/ (in my case $CATALINA_HOME/conf/Catalina/localhost/) create a file called “MyProject#photos.xml“.  The value before the # is your webapp name and the value after is the path you want added.
  • In this file add: <Context docBase=”d:\photos” /> and save.
  • Restart Tomcat.

Note for GWT:  I can’t seem to get this working with the embedded Tomcat that comes with GWT.  If anyone knows a simply way, please let me know.  In the mean time I’m using full Tomcat with the noserver option, and for the name of my context file I’m putting the full package name.  For example: com.nick.MyProject#photos.xml


3 Comments

  1. Daniele says:

    Hi.
    I’ve tried your solution but not work for me.
    I’ve a directory in /opt/tomcat/webphotogalleryIMG wich contain some photo.
    I’ve created a Context file like this WebPhotoGallery#webphotogalleryIMG.xml and put in
    When I put on browser this URL http://localhost:8080/WebPhotoGallery/webphotogalleryIMG/ tomcat response resource not avaible.

    Sorry for my english!

    Daniele

  2. Daniele says:

    I’ve resolved.
    Tomcat see directory.
    Is directory listing disabled on tomcat.
    if I put on url bar http://localhost:8080/WebPhotoGallery/webphotogalleryIMG/ I don’t see anything, but if I set http://localhost:8080/WebPhotoGallery/webphotogalleryIMG/ it’s appear.
    Thanks!

    Daniele

  3. Eric says:

    This is all true but if you are using Netbeans then you have to create “MyProject#photos.xml“ file in CATALINA_BASE not CATALINA_HOME. We use CATALINA_HOME only if CATALINA_BASE is not set.
    Sorry 4 my english:)

Leave a Reply