• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Adding external Directory to Tomcat 7

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 28313
207
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
It's a tiny ad. At least, that's what she said.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic