If you use Spring MVC for sites, you have likely run into a problem when you deploy.  How do you make the "welcome file" (for example, index.html) go through Spring and not your web server?  There is an easy way, and it does not involve extra files, javascript, or meta tags.

Let’s say you have a Spring web application called "mywebapp" on your site "mywebsite.com".  Your Spring dispatch servlet is mapped to capture *.html urls.  You want to have a welcome file of index.html, so that if a user just types in your webapp name, they go to your Spring handled index.html.  For example, you have these entries in your web.xml file:

<web-app>

<!-- other stuff here -->

    <servlet-mapping>
        <servlet-name>dispatch</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
    
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
  
<!-- more stuff -->      
    
</web-app>    

 

But, if your user goes to url http://mywebsite.com/mywebapp/, you will get a 404 error.  The problem?  The welcome file redirect bypasses the normal dispatch servlet mapping, so it isn’t used.  This happens in most popular web application containers including Tomcat and Jetty.

There are lots of ways around this, the most common being to add a static index.html in your webapp that just redirects to your Spring home page.  That is annoying, and not search engine friendly.  Instead, there is an easy trick:  add a dispatch servlet mapping for the url of your welcome file (that is, /index.html).

<web-app>

<!-- other stuff here -->

    <servlet-mapping>
        <servlet-name>dispatch</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
    
    <servlet-mapping>
        <servlet-name>dispatch</servlet-name>
        <url-pattern>/index.html</url-pattern>
    </servlet-mapping>    
    
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
  
<!-- more stuff -->      
    
</web-app>    

 

Works like a charm!  Now http://mywebsite.com/mywebapp/  will properly go to your mapped Spring controller.

Note: This article originally appeared in my Java blog (javanature.com).  I’m shutting that one down, so I’m moving the good stuff that is left over here!

11 Responses to “Spring Welcome File without redirect”

  1. Arjan Engbers says:

    Works like a charm, helps me a lot, thanks.

  2. theToolman says:

    Thanks for this tip. Here is a simplification you can make if you use the servlet 2.5 specification:

    dispatch
    *.html
    /index.html

  3. theToolman says:

    <servlet-mapping>
    <servlet-name>dispatch</servlet-name>
    <url-pattern>*.html</url-pattern>
    <url-pattern>/index.html</url-pattern>
    </servlet-mapping>

  4. Thanks for your hack, it works great! Sad fact that the welcome file definition bypasses the servlet mapping. Is this a bug?

    Cheers

    Marc

  5. Thanks,

    index.html note that there is no slash. When I appended a slash that causes a blank page. web.xml is very fragile

  6. wp swallowed my tags, I was talking about index.html in the welcome-file tag

  7. I tried to deploy to Glassfish 3 and I got this error message: “There is no web component by the name of ‘dispatch’ here.. ” Basically I believe that it’s looking for a servlet with that name and it cannot find it. How do I fix it?

  8. Good one

  9. Danke.

Leave a Reply

Your email address will not be published. Required fields are marked *

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