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) {
@Override
public void respond(RequestCycle requestCycle) {
WebResponse response = (WebResponse) requestCycle.getResponse();
response.reset();
response.getHttpServletResponse().setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.redirect(url);
}
};
getRequestCycle().setRequestTarget(target);
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