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.setHeader(“Location”, “”);

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


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


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 site uses a redirection Controller to serve its home page. Here are the relevant entries in its web.xml:


Redirects directory requests to the home page Action.

The URL of the home page, as an Action.



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


import hirondelle.web4j.util.Util;
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 {
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);

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