Saturday, April 17, 2010

Serving static resources in Glassfish 3

I was playing around with the Google Maps API, when I found that I can't access my Javascript files in Glassfish. The JSPs were working fine, so I guess it's some URL handling thing in web.xml. As it turns out, there's a default servlet in Glassfish that should be used to serve static resources (like CSS, Javascript, PDF, image files, etc). Digging into ${GLASSFISH_HOME}/glassfish/domains/domain1/config, I found the default-web.xml. Under the section called "Built In Servlet Definitions", there's this XML snippet:


default
org.apache.catalina.servlets.DefaultServlet

debug
0


listings
false

1


I copied this verbatim into my web.xml.

Now, I want the default servlet to handle all the accesses to the static resources. I tried to map URLs with certain prefixes to this servlet like so:


default
/js/*
/css/*
/img/*


Not sure what the problem is, but it doesn't work. I'm forced to try extension matching instead:


default
*.gif
*.jpg
*.png
*.js
*.css
*.txt
*.pdf


Well, now that works, but it's not an elegant solution. Imagine, if I want to serve a new kind of file, say an Excel file, I have to remember to include the extension in web.xml, besides just throwing the file into the correct folder. I will have to relook into this again, but for now, I'm just happy it works :)

No comments:

Post a Comment