N. wordan N. wordan - 2 months ago 8
Apache Configuration Question

How to specify my database directory to apache tomcate

Basically I have a java servlet web app which uses apache tomcat v9.0 as it's server, The problem is that I need to save my database in file system and specify that if a user requested something like

localhost/db/image.png
apache should know that db is located at file system and reply back to user with image.

Thanks for all replies:)

Answer

Tomcat can be configured to read files from anywhere on disk and serve them on a specific URL. This configuration is completely separate to your application config – you could, if you wanted, just start Tomcat and have it serve static files from disk with no webapps running. The configuration is in Tomcat’s server.xml configuration file and looks like this (the bit that needs to be added is in red):

<Host appBase="webapps"
           autoDeploy="false" name="localhost" unpackWARs="true"
           xmlNamespaceAware="false" xmlValidation="false">
    ...
    <Context docBase="/home/stuff" path="/static" />

  </Host>

A element is added inside the element. Context has two attributes: docBase is the directory on disk that contains your static files and path is the URL that you want to serve the files on.

Make sure that Tomcat has access to read the files in docBase.

Using the example server.xml snippet above, if I had an image in /home/stuff called steve.jpg I would be able to access it via my local Tomcat instance using http://localhost:8080/static/steve.jpg.