Jetty_welcome-file_mapping

Quick Profile

Alex Ooi Profile Picture
Hometown: Melbourne, Australia
Specializations: Java, Ruby on Rails
University: Software Engineering & Economics, Melbourne University
High School: VCE, Melbourne High School
Links:
 

Popular Articles

Thursday, April 14, 2011

Recently I had to map a servlet as a "welcome-file" in my servlet 2.4 web.xml. This should be straightforward, however I couldn't get it to work for quite awhile. The cause of the problem turned out to be obvious, but rather annoying, in retrospect...

The Code

I'm using Stripes as the java MVC framework, which involves a simple configuration in the web.xml to enable the stripes servlet to handle certain paths. Also, I'm using Jetty in my dev environment as a light-weight java servlet container. In production I'm using Tomcat. This turned out to be the source of the problem. The code for the web.xml is below:



    ....

    
        StripesDispatcher
        net.sourceforge.stripes.controller.DispatcherServlet
        1
    

    
        StripesDispatcher
        /home/*
    

    
        home
    

    ....



So this all looks ok, yeh? The welcome file is specified as a string that should match the servlet-mapping's url-pattern. The request would get forwarded to the StripesDispatcher servlet which will then eventually get handled by the correct ActionBean. The spec for the web.xml is defined at the top in the web-app tag as being of the servlet 2.4 spec which supports mapping servlets as the welcome-file. Also, both the version of jetty and Tomcat that I was using implement the servlet 2.4 spec.

However, upon loading up my application in jetty and navigating to application in a browser, jetty simply gave me a directory listing. It was clear that the welcome-file mapping wasn't working. At first I thought I got something in the web.xml wrong. After scanning many many articles and discussions on the web about mapping the welcome-file to a servlet I eventually determined that there was absolutely nothing wrong with my configuration.

Not turned on by default

Basicly, the problem is described by this document on the jetty wiki. I had assumed that, as long as the servlet 2.4 spec was specified, then this particular feature of servlet 2.4 should be enabled by compliant containers. Turns out that you need to turn it on in Jetty as it is turned off by default. Brilliant. Why would they do this when the 2.4 spec says that welcome-file now supports servlet mapping? :( Bizarro ... but it wasted enough of my time mucking around with my code when there was nothing wrong with it in the first place ... Naturally, when I deployed my app to Tomcat the welcome-file forwarding to the Stripes servlet worked by default :P

oh well ...

Comments ...

Holy Guacamole! Nobody has commented on this post yet! Why don't you make a name for yourself and be the first to do so ... Any comment whatsoever, as long as it is non-defamatory, will be welcome!

Add a Comment

*
*
You must answer the following simple maths question before your comment will be accepted.
*