而不是使用過濾器,而不是JSP(否則您可能會冒險IllegalStateException
s)。而是使用重定向而不是轉發(否則舊URL將保留在地址欄中)。而是使用301重定向而不是(默認)302(否則,舊網址仍將被searchbots編入索引)。
所以,你需要創建一個Filter
其監聽的舊拓*.nsf
的url-pattern
並實現它基本上像如下:
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.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location", newUrl);
}
你看,我已經重建的URL包括查詢字符串(在?
之後的部分)。
理想情況下,如果新JSP文件的結構和命名方式與原來的完全相同,但僅限於不同的擴展名。這樣你就不需要映射,但只需要在URL上替換一個字符串就足夠了:
String newUrl = oldUrl.replace(".nsf", ".jsp");
太棒了,謝謝。它看起來像要走的路。由於託管服務器是Tomcat 4,因此我需要解決Tomcat的一個問題,它看起來像eclipse只支持5(或5.5)的Web.xml更改。但它看起來像我想要去的方式。 – Craig 2010-02-10 17:45:12