dev@glassfish.java.net

servlet mapping question

From: Bobby Bissett <bobby.bissett_at_oracle.com>
Date: Thu, 27 Oct 2011 14:20:05 -0400

Hi all,

I've asked some people this before, but wanted to try the whole group to see if there's something I've missed in the servlet spec. Maybe this has changed in 3.0. I've seen this question come up a few times in various forums, and wanted to get a definitive answer about whether or not it's possible. Imagine I want to do the following:

1. Deploy app at context root /
2. In web.xml, map /* to servlet MyServlet
3. *Not* map a directory of static content, for instance /static/a.html and /static/b.html (in a real app, these would be js files).

If there are only two static files, this works:

<?xml version="1.0" encoding="UTF-8"?>
<web-app .... version="3.0">

    <servlet>
        <servlet-name>MyServlet</servlet-name>
        <servlet-class>com.example.MyServlet</servlet-class>
    </servlet>

    <servlet>
        <servlet-name>StaticA</servlet-name>
        <jsp-file>/static/a.html</jsp-file>
    </servlet>
    
    <servlet>
        <servlet-name>StaticB</servlet-name>
        <jsp-file>/static/b.html</jsp-file>
    </servlet>

    <servlet-mapping>
        <servlet-name>MyServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>StaticA</servlet-name>
        <url-pattern>/static/a.html</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>StaticB</servlet-name>
        <url-pattern>/static/b.html</url-pattern>
    </servlet-mapping>
</web-app>

But that's hardly scalable. Is there some way to do something like this so that any call to /static/X goes to file X?

    <servlet>
        <servlet-name>staticstuff</servlet-name>
        <jsp-file>/static/*</jsp-file> <!-- this is the part I'm making up
    </servlet>

    <servlet-mapping>
        <servlet-name> staticstuff </servlet-name>
        <url-pattern>/static/*</url-pattern>
    </servlet-mapping>

Obviously the above doesn't work, but it illustrates what I mean. Can I map /* to a servlet *except* /something/* ? I know the workaround is to move the servlet to /something and just map /somthing/* to it. Tomcat has this "DefaultServlet" class that is used for serving static content. Is there some portable EE equivalent?

Thanks,
Bobby