Wednesday, October 11, 2006

Configuring Tomcat to serve static files

For a long time now, I have been trying to figure out how to specify a default servlet in Tomcat 5 and still allows for static files to be served.

From all of my readings, I have noticed that in order to have a default servlet and serve static files, I would have to serve the files via my servlet. I didn't like this solution. Finally, I have discovered a work around...

To outline the problem, imagine that your web.xml file contains the following:

<servlet-mapping>
<servlet-name>My Servlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

Now all requests to your webapp are going to go through 'My Servlet'. So if you ask for a file like http://localhost:8080/your_app/myStyleSheet.css, Tomcat will look at the request and forward it to 'My Servlet'.

Now unless that particular servlet contains code to handle the case for myStyleSheet.css, nothing will happen (depends on your 'My Servlet' code of course; 404 might be returned, etc).

So one work around that I have found is to enter the following in your tomcat/webapps/your_app/WEB-INF/web.xml:

<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>

With that code all requests for a file ending in the prefix css will not be sent to 'My Servlet'. Instead Tomcat will look for the file in your web apps home directory and if found, serve it, otherwise a 404 is returned.

Now that I have found this one solution, it seems to me like it was obvious (maybe that's why I couldn't find it in all my web searching with google) ...


16 comments:

Anonymous said...

Nice. Everytime I attempted what you mentioned, people said that it couldn't be done, or that I should remove the default servlet and use error-pages to redirect to the default servlet.

tfulton said...

i'm a bit late on this one, but this solved my problem!! much easier than some of the more complicated solutions offered by people. thanks!!

todd

Jules said...

Thanks. Just so you know, the same trick worked perfectly with Jetty, too.

Anonymous said...

Thanks, that's awesome!!

ChocJunkie said...

Thx! :)

Unknown said...

Working like a sharm. Thanks!

Joe said...

Thanks a lot. Worked for me too on Tomcat 6.

Anonymous said...

Thank you, very helpful!

Diego said...

Thanks, I was trying with something like "/js/*" but it didn't work, although "*.js" worked fine. It's important to leave the static content outside WEB-INF

Anonymous said...

That is not a "workaround" - it is a SOLUTION!

Excellent!

Anonymous said...

In older versions of tomcat, this can be a security risk if it makes /WEB-INF accessible.

Unknown said...

Thx a lot, Ed!!! I was searching for solution for a whole week!

Anonymous said...

Wow! I searched for it for 1 week!

Kushi said...

I tried it on glassfish 3.1.2
Something like this :



default
/index.jsp





but ended up with : SEVERE: There is no web component by the name of default here.

Unknown said...

Many many thanks, I have been looking for this for a long long time.

Ed said...

I have to admit, I am a bit surprised that after 7 years, serving static files is still tough... Glad it helped you out!