2017-05-18 56 views
0

我想自定義tomcat7錯誤頁面。我需要爲所有的上下文路徑全局設置,所以我與全球/etc/tomcat/web.xml文件中的配置結束:無法自定義tomcat7 404錯誤頁面

<?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"> 
<error-page> 
     <error-code>404</error-code> 
     <location>/custom-pages/error.jsp</location> 
</error-page> 

Tomcat的成功在返回頁面當我問它:

curl -s 127.0.0.1:8080/custom-pages/error.jsp 

<html><body>Sorry, the page you requested were not found.</html></body> 

然而,當我要求的一些頁面不存在,我得到一個默認的錯誤頁面自帶的tomcat,但不是我一個:

curl 127.0.0.1:8080/not-exist 
<html><head><title>Error report</title></head> 
<body><h1>HTTP Status 404 - /not-exist</h1></body> 
</html> 

這裏的Tomcat Web目錄的佈局:

tree /usr/share/tomcat7/webapps/ 
/usr/share/tomcat7/webapps/ 
└── ROOT 
    └── custom-pages 
     └── error.jsp 

自定義錯誤頁的內容:

cat /usr/share/tomcat7/webapps/ROOT/custom-pages/error.jsp 
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 
<html><body>Sorry, the page you requested were not found.</body></html> 

的任何想法?提前致謝。

+0

您的結束標籤按預期順序切換。可能是一個錯字,並不確定會影響任何內容,但值得注意。 ''shoudl be'' – zzevannn

+0

嗨!是的,這是一個錯字。無論如何感謝您注意到這一點。 –

回答

0

好吧,同時我找到了答案。我不得不發表評論org.apache.catalina.valves.ErrorReportValve指令裏面/etc/tomcat7/context.xml它的確有竅門。看起來像這個指令「覆蓋」任何自定義錯誤頁面!希望對其他人有用...:

<!-- 
<Valve className="org.apache.catalina.valves.ErrorReportValve" 
showReport="false" showServerInfo="false" /> 
-->