linux - 无法自定义tomcat7 404错误页面

标签 linux tomcat7

我正在尝试自定义 tomcat7 错误页面。我需要所有上下文路径的全局设置,所以我最终在全局 /etc/tomcat/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">
<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>

然而,当我请求一些不存在的页面时,我得到一个默认的错误页面,但不是我的页面:

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 网络目录的布局:

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>

有什么想法吗?提前致谢。

最佳答案

好吧,同时我找到了答案。我不得不在 /etc/tomcat7/context.xml 中注释 org.apache.catalina.valves.ErrorReportValve 指令,它成功了。看起来这个指令“覆盖”了任何自定义错误页面!希望对其他人有用......:

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

关于linux - 无法自定义tomcat7 404错误页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44052425/

相关文章:

c - 如何使用 pcap 从多个接口(interface)捕获流量

linux - 如何防止在 ZSH 中执行命令?

java - 重启嵌入式tomcat

tomcat 7 无法访问且无法关闭

sql - 我无法在我的项目中上传文件

java - 错误 java.lang.NoClassDefFoundError : org/apache/http/client/methods/HttpUriRequest

javascript - 如何从另一台服务器获取文件?

php - 如何记录/查明哪个 PHP 脚本使用了大量内存?优化问题

linux - Ubuntu Shell 脚本问题 : convert output of command into variable

linux - 重命名多个顺序文件扩展名