Wicket – Sending a 301 redirect

This post describes how to send a HTTP 301 redirect in the Java Wicket Framework.

HTTP redirect intro

There are two types of HTTP redirects:
-“HTTP 1.1 301 Moved Permanently”
-“HTTP 1.1 302 Found”, previously called “HTTP 1.1 302 Moved Temporarily”

A 301 indicates that the requested resource has been assigned a new permanent URI and any future references to this resource SHOULD use one of the returned URIs.
Where a 302 indicates that the requested resource resides temporarily under a different URI.

For a normal web browser it doesn’t matter which type of redirect is send to the browser, because the result is the same: the user is redirect to a different URI. But for Search Engines (SE’s) it does (or might) matter, at least if you want the correct URL to be indexed. If you’re using a 301, than you’re telling SE’s that they should use the new URI. If you’re sending a 302 then you’re telling that they should use the old URI.

301 using Wicket

In Wicket, you can send a 301 using:

getRequestCycle().setRequestTarget(new RedirectRequestTarget("/path/to/legacyJspFile.jsp"));

302 using Wicket

The problem is that the redirect method always uses a 302. If you want to perform a 301, you can use this code, which overrides the respond method:

RedirectRequestTarget target = new RedirectRequestTarget(url) {
  public void respond(RequestCycle requestCycle) {
    WebResponse response = (WebResponse) requestCycle.getResponse();

This will cause the RedirectRequestTarget.respond(RequestCycle requestCycle) method to be called, which will get a WebResponse object using the requestCycle object, and it will call the WebResponse.redirect(String url) to set the redirect.

More info:

  • http://wicket.apache.org/docs/1.4/
  • http://www.w3.org/Protocols/rfc2616/rfc2616.txt
  • http://cwiki.apache.org/WICKET/how-to-redirect-to-an-external-non-wicket-page.html

2 thoughts on “Wicket – Sending a 301 redirect”

  1. Hi there, I think you probably have mixed up the headlines “301 using Wicket” and “302 using Wicket”, the first one does a 302 for me (Wicket 1.3.4)

    Also, I tried the 2nd snippet (called from setHeaders), which results in a 302 as well… within a page’s overwritten setHeaders method, I am using the following code to have a 301 redirect:

    HttpServletResponse hsr = response.getHttpServletResponse();
    hsr.addHeader(“Location”, url);

  2. I just had this problem with Wicket 1.4.1 and came across this blog post. Even with mixed up headlines and 302 in both solutions, it gave me the correct head start, so thanks, JD.

    Sending a real 301 is easily accomplished by replacing line 7 (the response.redirect() call) of the code with

    response.setHeader(“Location”, url);

Leave a Reply

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