I’m trying to implement a 301 redirect from within JSF but when I firebug it I always see a 302 being executed. Would someone be able to show me how to alter the method below to accomplish a 301?
/**
* Redirect to specified destination url
*
* @param destination
*/
public static void permanentRedirect(String destination) {
final ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
try {
HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
if (!response.isCommitted()) {
externalContext.redirect(destination);
}
} catch (IOException e) {
LOGGER.debug("Could not redirect to " + destination);
}
}
You shouldn’t invoke
externalContext.redirect(destination). It will override the status to 302. You should manually set theLocationheader and complete the response.