Meaningless Drivel is fun!
The moose likes Tomcat and the fly likes Adding external Directory to Tomcat 7 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Adding external Directory to Tomcat 7" Watch "Adding external Directory to Tomcat 7" New topic

Adding external Directory to Tomcat 7

Thomas Albrecht

Joined: Aug 31, 2010
Posts: 2

I'm using Tomcat 7 and want to at an external Directory to my Webroot/Webapps.
For example I have my Webroot in /mywebroot and I have the external Directory in /datas/thedirectory.
Now I want that if someone get a link to a file a.pdf which is in /datas/thedirectory and it looks like:

How I have to configure Tomcat 7 to do this

Thanks for your help in advance :-)

greetings, itachy
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17421

Welcome to the JavaRanch, Thomas.

Tomcat is not a file server. It's a web application server. A web server accepts URLs and routes them to be processed. The default processor will convert the URL to a relative pathname, attempt to locate a resource at that path location, and, if it finds one, copy the contents of that resource to the response output stream going back to the user's browser. Subject to the restriction that /WEB-INF and its children cannot be copied.

According to the strict J2EE specification, a WAR isn't even a filesystem directory tree at all. It's a JAR (ZIP) file. However many appservers - including Tomcat - can explode the WAR into a designated directory or reference a pre-exploded WAR directory.

In practical terms, however, if you're careful, you can bend the J2EE standard to do what you want if your operating system is capable. This requires that you create a symbolic link in your exploded WAR to the external directory and it requires that you configure Tomcat to allow it to lookup resources through symbolic links. That feature is turned off by default because it's a security risk.

This approach works, but has various flaws. As mentioned, your OS has to support symbolic links, symbolic links mean a security risk. Plus a true WAR doesn't have any way to indicate symbolic links. A cleaner approach requires writing (or stealing) a simple file-copy servlet. All the servlet does is convert a URL to a filename path and copy the contents of the file at that path to the response stream just like the Tomcat default processor does. For more flexibility you can provide the base directory name as a parameter and supply it in your web.xml file, a properties file, JNDI or whatever, so that you can switch to another directory without recompiling - for testing purposes, for example.

An IDE is no substitute for an Intelligent Developer.
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

You can also create a new context pointing to an external folder - with a context.xml - and then use this new context as your file server.

Regards, Rene Larsen
Dropbox Invite
I agree. Here's the link:
subject: Adding external Directory to Tomcat 7
It's not a secret anymore!