2010-09-21 102 views

回答

3

沒有辦法像使用Apache一樣簡單地設置它。最接近的做法是使servlet或jsp處理重定向,然後將其映射到要重定向的URL。在servlet或JSP會做這樣的事情:

response.setStatus(301); 
response.setHeader("Location", "http://www.example.com/redirect-to-here.html"); 
0

重定向整個文件夾到您需要同時獲得JSP和配置呼籲404

這個JSP在index.jsp您需要修改您的新位置重定向行爲。下面的代碼將從OldApp文件夾重定向到同一臺服務器上的NewApp文件夾。

的index.jsp:

<!DOCTYPE html> 
<html lang="en-US"> 
<head> 
    <meta charset="UTF-8" /> 
    <meta name="viewport" content="width=device-width, initial-scale=1" /> 
    <title>301 Moved</title> 
</head> 
<%@ page import="org.apache.catalina.util.RequestUtil" %> 
<% 
    // get the requested URI 
    //String requestedLocation = request.getRequestURI(); 
    // original request 
    String requestedLocation = RequestUtil.filter((String) request.getAttribute("javax.servlet.error.request_uri")); 
    // rewrite to new location 
    String newLocation = requestedLocation.replaceAll("^/OldApp", "/NewApp"); 
    // add query string 
    String query = request.getQueryString(); 
    if (!query.isEmpty()) { 
     newLocation = newLocation + '?' + query; 
    } 

    // 301 - permanent redirect 
    response.setStatus(response.SC_MOVED_PERMANENTLY); 
    response.setHeader("Location", newLocation); 
%> 
<body> 
    &rarr; <a href="<%=newLocation%>"><%=newLocation%></a> 
</body> 
</html> 

WEB-INF/web.xml中:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
         http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    version="3.0" 
    metadata-complete="true"> 

    <error-page> 
    <error-code>404</error-code> 
    <location>/index.jsp</location> 
    </error-page> 
</web-app>