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!

Storing the contents of a URL to a file

This example shows how to store the contents of a URL to a File.

URLFetcher.java:

import java.io.*;
import java.net.URL;

public class URLFetcher {
  private String urlString;
  private String fileName;
  
  public URLFetcher(String urlString, String fileName) {
    this.urlString = urlString;
    this.fileName = fileName;
  }

  public void fetch() throws Exception {
    byte buffer[] = new byte[1024]; // change 1024 to better match your needs
    int numRead = 0;
    URL url = new URL(this.urlString);
    OutputStream os = new FileOutputStream(new File(this.fileName));
    InputStream is = url.openStream();
    while ( (numRead = is.read(buffer,0,1024)) != -1) {
      os.write(buffer, 0, numRead);
    }
    is.close();
    os.close();
  }
}

Using the URLFetcher:

URLFetcher urlFetcher = new URLFetcher("myUrl", "myFileName");
try {
  urlFetcher.fetch();
} catch(Exception e) {
  e.printStackTrace();
}

 

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!

Strip HTML Tags in Java

I came upon a situation in which I needed to find out if String was "really" empty (not just spaces and junk).  The problem was that the String might have empty HTML tags, like <p></p>.  This handy function removes all the HTML tags, leaving any real content.

function removeHtmlTags(String original) {
    return original.replaceAll("\<.*?>", "");
}

The results?

removeHtmlTags("<p></p>")  returns the empty string, but

removeHtmlTags("<p>foo</p>")  returns "foo"

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