Java web.xml 自定义错误页面不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19902579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
web.xml custom error page not working
提问by dono
I'm working in Eclipse+tomcat. My custom made static error page is not showing up in tomcat when i click on url for non-existing pages. generated-error.html has a simple img src="404_man.jpg" tag. But I keep getting the error page shown on the pic.
我在 Eclipse+tomcat 中工作。当我单击不存在页面的 url 时,我的自定义静态错误页面没有显示在 tomcat 中。generate-error.html 有一个简单的 img src="404_man.jpg" 标签。但我不断收到图片上显示的错误页面。
My servlet version is 3.0 if i view it from manifest.mf. I think it's the right document to look for Servlet version?
如果我从 manifest.mf 查看它,我的 servlet 版本是 3.0。我认为这是寻找 Servlet 版本的正确文档?
Pls don't leave me hanging...
请不要让我挂...
采纳答案by Imran
You should add the error code
or exception-type
tags in web.xml for particular error...
您应该在 web.xml 中为特定错误添加error code
或exception-type
标记...
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/error.jsp</location>
</error-page>
回答by isnot2bad
Before servlet version 3.0 you'll have to specify an error code using the error-code
tag. It is not required since 3.0. Check if you really use this version. Your web-app
tag should look like:
在 servlet 3.0 版之前,您必须使用error-code
标记指定错误代码。从 3.0 开始就不需要了。检查您是否真的使用此版本。您的web-app
标签应如下所示:
<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">
回答by dono
Interesting, I added <exception-type>java.lang.Exception</exception-type>
, deployed tomcat, deleted exception type back, restarted tomcat with no <error-code>
line, and it worked. still no idea what was the reason behind the problem that cost me one whole day.
有趣的是,我添加了<exception-type>java.lang.Exception</exception-type>
,部署了tomcat,删除了异常类型,重新启动了没有<error-code>
线的tomcat ,它工作了。仍然不知道让我花了一整天的问题背后的原因是什么。
EDIT:Restart again, and now it doesn't work...
编辑:再次重新启动,现在它不起作用......
I restarted again, but still doesn't work, is this a cache problem? it's definitely something that i have no idea of...
我又重启了,还是不行,这是缓存问题吗?这绝对是我不知道的事情......
回答by Claudiu-Florin Stroe
In your error page you have to add isErrorPage="true".
在您的错误页面中,您必须添加 isErrorPage="true"。
回答by Kris
Try from another browser, instead of internal eclipse browser. And make sure error.jsp is on same level as web-inffolder.
尝试使用其他浏览器,而不是内部 Eclipse 浏览器。并确保 error.jsp 与web-inf文件夹在同一级别。
回答by Passionate Life
For all of you looking for a simple straight forward solution
对于所有正在寻找简单直接解决方案的人
Please do the following .
请执行以下操作。
1) try running in firefox
1) 尝试在 Firefox 中运行
2) try running in chrome
2)尝试在chrome中运行
root cause : The root cause is internet explorer , since eclipse takes
根本原因:根本原因是 Internet Explorer,因为 eclipse 需要
IE as default browser you keep getting http error . please do as I said
IE 作为默认浏览器,您不断收到 http 错误。请按我说的做
It will work for you . if you like my solution do comment on it : )
它会为你工作。如果您喜欢我的解决方案,请对其发表评论:)
回答by KARANJ
If you want static error page, Than you need to do:
如果你想要静态错误页面,那么你需要做的:
1.include similar to below given example in web.xml file:
1.在 web.xml 文件中包含类似于下面给出的示例:
<error-page>
<error-code>404</error-code>
<location>/error-404.jsp</location>
</error-page>
- in static error file (i.e. error-404.jsp) add "isErrorPage="true" in page-dependent attributes. See example:
- 在静态错误文件(即 error-404.jsp)中,在页面相关属性中添加“isErrorPage="true”。参见示例:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" isErrorPage="true"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" isErrorPage="true"%>
- Try firefox or chrome. Many times it worked for me.
- 试试火狐或铬。很多时候它对我有用。
回答by Tarun Verma
It seems Internet Explorer issue. Try disabling the option - "Show friendly HTTP error messages" in "Advanced" tab of "Internet Options".
似乎是 Internet Explorer 问题。尝试禁用选项 - “Internet 选项”的“高级”选项卡中的“显示友好的 HTTP 错误消息”。