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:

<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

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:

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

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

<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
<url-pattern>*.jpg</url-pattern>
<url-pattern>*.png</url-pattern>
<url-pattern>*.js</url-pattern>
<url-pattern>*.css</url-pattern>
<url-pattern>*.txt</url-pattern>
<url-pattern>*.pdf</url-pattern>
</servlet-mapping>

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 :)

Tags: , ,

Related posts:

  1. Problems creating a JDBC resource with Glassfish 3 and PostgreSQL 8.4 drivers What I initially thought was a straight-forward task turns out to be more complex than...
  2. Struts DispatchAction – unspecified method I was going through a tutorial about DispatchAction and trying to define a default page...
  3. Installing PHP 5.2.5 on Windows XP for Tomcat 6.0.13 – Issues resolved I was playing around with the idea of doing a small personal PHP project, and...
  4. Debugging a JSP compilation problem on Tomcat 6 I was doing some experimentation with Maven and Tomcat 6 when I hit this error....
  5. Maven2 – installing 3rd party JARs Have a little time these few days, trying to get familiarised with Maven2. Not for...

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>