Consider Controllers for redirects

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:

<servlet>
 <servlet-name>RedirectWelcomeFile</servlet-name>
 <description>
  Redirects directory requests to the home page Action.
 </description>
 <servlet-class>hirondelle.jp.redirect.WelcomeFileController</servlet-class>
  <init-param>  
   <param-name>Destination</param-name>
   <param-value>http://www.javapractices.com/home/HomeAction.do</param-value>
    <description>
     The URL of the home page, as an Action.
    </description>
  </init-param>  
 </servlet>

<servlet-mapping>
 <servlet-name>RedirectWelcomeFile</servlet-name>
 <url-pattern>/welcome</url-pattern>
</servlet-mapping>

<!-- the welcome-file points to the servlet, not to a file -->
<!-- note the slash does not appear here -->
<welcome-file-list>
 <welcome-file>welcome</welcome-file>
</welcome-file-list>
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.
* 
* <P>Configuration in web.xml :<br>
* 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);
}
 



See Also :
Forward versus redirect
Would you use this technique?
Yes   No   Undecided   
© 2014 Hirondelle Systems | Source Code | Contact | License | RSS
Individual code snippets can be used under this BSD license - Last updated on September 21, 2013.
Over 2,000,000 unique IPs last year - Built with WEB4J.
- In Memoriam : Bill Dirani -