0% found this document useful (0 votes)
195 views3 pages

Serving Static Content Outside of The WAR Using Apache Tomcat

This document discusses how to configure Apache Tomcat to serve static content located outside of the WAR file, such as images, CSS files, HTML pages, and more. This is done by adding a <Context> element to the server.xml file that defines the directory on disk containing the static files (docBase) and the URL path under which they will be served (path). For example, an image located at /home/stuff/image.jpg could be served at http://localhost:8080/static/image.jpg. This allows static files to be updated without redeploying the WAR file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
195 views3 pages

Serving Static Content Outside of The WAR Using Apache Tomcat

This document discusses how to configure Apache Tomcat to serve static content located outside of the WAR file, such as images, CSS files, HTML pages, and more. This is done by adding a <Context> element to the server.xml file that defines the directory on disk containing the static files (docBase) and the URL path under which they will be served (path). For example, an image located at /home/stuff/image.jpg could be served at http://localhost:8080/static/image.jpg. This allows static files to be updated without redeploying the WAR file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Serving static content outside of the WAR using Apache Tomcat

1 sur 3

https://www.moreofless.co.uk/static-content-web-pages-images-tomca...

How to serve static content from a location on disk, that is outside of the WAR file, using
Apache Tomcat 7. This method can be used to serve images, JavaScript, CSS, JSON,
PDFs and even static HTML web pages.
Tomcat will serve any static content from a WAR file using the DefaultServlet
(http://tomcat.apache.org/tomcat-7.0-doc/default-servlet.html). This works great for
serving files that are bundled with your Java code inside of a WAR file its fast enough
for most purposes and it just works, but with one major drawback: you have to re-deploy
the WAR file it you want to add or amend a static file. That may work for you but if you
are working in a development process where releasing new versions of your app requires
sign-offs and rounds of testing then it doesnt make sense to go through that process just
to update a single image file because someone wants a slightly darker blue background
on the contact page.
We can be a bit more flexible.

24/09/2015 13:41

Serving static content outside of the WAR using Apache Tomcat

2 sur 3

https://www.moreofless.co.uk/static-content-web-pages-images-tomca...

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 Tomcats 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 <Context> element is added inside the <Host> 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 .

SERVING STATIC WEB PAGES


This method also works for serving HTML web pages and can be a great way to serve
unchanging web pages quickly from Tomcat without having to write any Java code.
This works exactly the same as the above image example. Tomcat would serve the
/home/stuff/hello.html

via http://localhost:8080/static/hello.html .

A QUICK NOTE ABOUT MIME TYPES


Tomcat serves the correct MIME type to the browser for static content when the type is
obvious from the name of the file being served for example steve.jpg is served as a
image/jpeg

and hello.html is served as text/html .


24/09/2015 13:41

Serving static content outside of the WAR using Apache Tomcat

3 sur 3

https://www.moreofless.co.uk/static-content-web-pages-images-tomca...

You may want to serve web pages using clean and SEO-friendly URLs, so instead
of http://localhost:8080/static/hello.html youd
have http://localhost:8080/static/hello Tomcat doesnt know the MIME type for a
file with no extension and so serves hello without a specified type. This is fine most of
the time as browsers are smart and realise that hello is an HTML document but if you
are serving content to something other than a browser (say someone elses code is reading
your content via an API) then you may need to be careful about MIME types.

24/09/2015 13:41

You might also like