How to use Tomcat 7 new aliases properly?

meduoliai picture meduoliai · Oct 22, 2012 · Viewed 8.5k times · Source

Tomcat 7 provide new aliases that allow to store static content outside the WAR File. I found some instructions how to do that in http://www.developer.com/java/web/article.php/3904871/Top-7-Features-in-Tomcat-7-The-New-and-the-Improved.htm part 4.

But it does not work for me.

I have two context XML files. One for my application (admin.xml) and one for my static resources (uploads.xml).

I did everything like mentioned in this tutorial but Tomcat does not see any static resources outside WAR file.

My static resources structure:

/home/user/admin-images/-
                   -234bF5_image/image.jpg
                   -572d44_otherImage/otherImage.jpg
                   -12A4uR_otherImage/otherImage.jpg

admin.xml:

<Context path="/admin" 
        docBase="path/to/application/target" reloadable="false">
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <Manager pathname="admin" />
</Context>

uploads.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/admin" aliases="/images=/home/user/admin-images">

</Context>

After context deploy images not reachable:

http://localhost:8080/admin/images/234bF5_image/image.jpg

when I try to reach this I get error that image can not be displayed.

Answer

meduoliai picture meduoliai · Nov 16, 2012

I did not manage to get it working. But I found another solution to serve static images outside web application.

You need define context in server.xml inside Tomcat configuration:

server.xml (this have to go into 'Host' tag):

...
...
<Host>
    ...
    ...
    <Context docBase="/home/user/admin-images/" path="/images" />
    ...
    ...
</Host>
...
...

That way I am able to reach static images like that: [http://localhost:8080/admin/images/234bF5_image/image.jpg]

My new question would be: This way to share static resources is safe? Does it have any disadvantages?