HTTP provides redirection headers. A main use case for these headers is to instruct browsers that a page has moved to a new URL, either temporarily or permanently. When a page is moved, any links to the old URL will be broken, unless a redirect is provided.

Here’s a snippet which performs a permanent redirection, using an HttpServletResponse object:

aResponse.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
aResponse.setHeader(“Location”, “www.javapractices.com/source/SourceAction.do”);

In the case of a temporary redirection, another style can be used instead, if desired:

aResponse.sendRedirect(“www.javapractices.com/source/SourceAction.do”);

Using a Controller servlet to perform redirections is often an attractive option.

Example

The Servlet API allows for welcome-file entries in web.xml. These items handle requests which specify only a directory, instead of a specific file or action.

A common requirement is to serve welcome files using a servlet instead of a static page. It’s true that a static page can indeed perform a redirect. However, that style seems to be slower. As well, it forces the quick display of an intermediate page before the “real” one, which seems to be a less pleasing experience for the user.

The javapractices.com site uses a redirection Controller to serve its home page. Here are the relevant entries in its web.xml:

RedirectWelcomeFile

Redirects directory requests to the home page Action.

hirondelle.jp.redirect.WelcomeFileController

Destination http://www.javapractices.com/home/HomeAction.do
The URL of the home page, as an Action.


RedirectWelcomeFile
/welcome




welcome


The actual Controller class is fairly simple. Note that it reads in the Destination setting from web.xml :


package hirondelle.jp.redirect;

import hirondelle.web4j.util.Util;
import java.io.IOException;
import java.util.logging.Logger;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Redirect requests for the home page to a specific Action.
*
*

Configuration in web.xml :
* This Controller servlet is mapped to '/welcome', and a corresponding welcome-file entry
* is listed as 'welcome', without the leading slash.
*/
public final class WelcomeFileController extends HttpServlet {

@Override public void init(ServletConfig aConfig) throws ServletException {
super.init(aConfig);
fLogger.config("WelcomeFile Controller - starting.");
fDestination = aConfig.getInitParameter("Destination");
if ( ! Util.textHasContent(fDestination) ) {
fLogger.severe("Destination URL needed, but not configured in web.xml.");
}
}

@Override public void destroy(){
fLogger.config("WelcomeFile Controller - ending.");
}

@Override protected void doGet(
HttpServletRequest aRequest, HttpServletResponse aResponse
) throws ServletException, IOException {
fLogger.fine("Redirecting directory request to new location : " + fDestination);
aResponse.setContentType("text/html");
aResponse.sendRedirect(fDestination);
}

// PRIVATE //
private String fDestination;
private static final Logger fLogger = Util.getLogger(WelcomeFileController.class);
}