views:

92

answers:

3

Hi I am upgrading a site, moving from Notes to Java JSP's and need to support a redirect from the old url's to the new ones. So /site/oldpage.nsf?home needs to redirect to /site/newpage.jsp.

In Tomcat I've set up an error-page to redirect: 404 /avi_redirect.jsp

in the avi_redirect.jsp I have :

pageContext.forward(  "/newpage.jsp" );

But as I need to do this for many pages I need to know the old page name ie /site/oldpage.nsf?home.
Any idea how I can get this? The response & headers don't have it. The query string has the params but not the url.

A: 

You need to maintain a mapping of old page url to new page url. You can keep this mapping in properties file, xml or database and the avi_redirect.jsp looks up this mapping and redirects to the new page.

Other thing you need to take is about the url parameters if they are same or not. If they are also not same then you need to a mapping for them as well.

Bhushan
The mapping only works if I know which page is missing from. That's what I can't find out.
Craig
+1  A: 

You should consider doing it in your DD, web.xml. And write a filter that will redirect the requests to the appropriate pages.

Adeel Ansari
ah, filter mapping? just finding out info about it now
Craig
Correct, brilliant. Let us know if you need further explanation.
Adeel Ansari
+1  A: 

Rather use a Filter for this, not a JSP (otherwise you may risk IllegalStateExceptions). 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");
BalusC
Craig