Rather use a Filter for this, not a JSP (otherwise you may risk IllegalStateException
s). Also rather use a redirect instead of a forward (otherwise the old URL will stay in the address bar). Also rather use a 301 redirect instead of a (default) 302 (otherwise the old URL will still be indexed by searchbots).
So, you need to create a Filter
which listens on an url-pattern
of the old extension *.nsf
and implement it basically like follows:
private static final Map<String, String> urlMapping = new HashMap<String, String>();
static {
urlMapping.put("/site/oldpage.nsf?home", "/site/newpage.jsp");
// Put more here.
}
public doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException) {
HttpServletRequest httpReq = (HttpServletRequest) request;
String oldUrl = httpReq.getRequestURL().append("?").append(httpReq.getQueryString()).toString();
String newUrl = urlMapping.get(oldUrl);
response.sendRedirect(newUrl, HttpServletResponse.SC_MOVED_PERMANENTLY);
}
You see that I already have reconstructed the URL to include the querystring (the part after ?
).
Ideally would be if the new JSP files are structured and named exactly the same way as originally, but only with a different extension. This way you don't need a mapping, but just a string replace on the URL would have been sufficient:
String newUrl = oldUrl.replace(".nsf", ".jsp");