Spring Welcome File without redirect

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!

18 thoughts on “Spring Welcome File without redirect”

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

    dispatch
    *.html
    /index.html

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

  3. Thanks,

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

  4. 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?

  5. What’s Happening i am new to this, I stumbled upon this I’ve discovered It positively useful
    and it has helped me out loads. I am hoping to contribute & help other customers like its aided me.
    Great job.

  6. Hi I am getting “requested resource not available ” for below web.xml, please let me know how can I map my welcome page and to connect with controller class also

    dispatcher
    org.springframework.web.servlet.DispatcherServlet
    1

    dispatcher
    *.htm

    userForm.jsp

  7. Hi admin, i must say you have high quality content here.
    Your website can go viral. You need initial traffic boost only.
    How to get it? Search for: Mertiso’s tips go viral

Leave a Reply

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