Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding external Directory to Tomcat 7

 
Thomas Albrecht
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

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:
http://host/mywebroot/thedirectory/a.pdf

How I have to configure Tomcat 7 to do this

Thanks for your help in advance :-)

greetings, itachy
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18167
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic