java web.xml 中的错误页面给出 404
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5441131/
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
error page in web.xml giving 404
提问by vaibought
In one of my project, I need to handle the 404(Resource not found) and 403(Access Denied). I am giving the configuration in web.xml as
在我的一个项目中,我需要处理 404(找不到资源)和 403(拒绝访问)。我将 web.xml 中的配置作为
<error-page>
<error-code>404</error-code>
<location>/WEB_INF/jsp/web/exception/weberror.jsp</location>
</error-page>
<error-page>
<error-code>403</error-code>
<location>/WEB_INF/jsp/web/exception/accessDenied.jsp</location>
</error-page>
</error-page>
Now when the access denied generates, browser is showing me native 404. I also remove the friendly error page setting in browser but on removing that I am getting a blank page.
现在,当访问被拒绝生成时,浏览器向我显示本机 404。我还删除了浏览器中的友好错误页面设置,但删除后我得到了一个空白页面。
My jsps are in WEB-INF folder. The path where all jsps are places is
我的 jsps 在 WEB-INF 文件夹中。所有jsps所在的路径是
/WEB_INF/jsp/
Please help me out. If I am missing something?
请帮帮我。如果我错过了什么?
回答by Axel Knauf
Files under /WEB-INF/
are protected from direct access by the client, this is defined in the Java EE specification. So your error pages will have to reside outside this directory. Try moving them to a dedicated location, e. g. /errors/404.jsp
.
下面的文件受到/WEB-INF/
保护,不能被客户端直接访问,这是在 Java EE 规范中定义的。所以你的错误页面将不得不驻留在这个目录之外。尝试将它们移动到专用位置,例如/errors/404.jsp
。
回答by Roland Hu?
What's about using WEB-INF
instead of WEB_INF
in your web.xml ?
在 web.xml 中使用WEB-INF
而不是什么WEB_INF
?
回答by Durgadas Kamath
The problem is that you are using WEB_INF instead of WEB-INF. all the resources mentioned under WEB-INF are protected from client and cannot be directly accessed so its always a good practice to move it out of the WEB-INF.
问题是您使用的是 WEB_INF 而不是 WEB-INF。WEB-INF 下提到的所有资源都受到客户端保护,无法直接访问,因此将其移出 WEB-INF 始终是一个好习惯。
Thanks
谢谢
回答by Gaurav
The error pointed above looks on web.xml as cvc-complex-type.2.4.a: Invalid content was found starting with element 'location'.
上面指出的错误在 web.xml 上看起来为 cvc-complex-type.2.4.a:发现以元素“位置”开头的无效内容。
<error-page><location>/error</location></error-page>
Error which it generates is
它产生的错误是
{"java.sun.com/xml/ns/javaee":error-code, "java.sun.com/xml/ns/javaee":exception-type} is expected.
{"java.sun.com/xml/ns/javaee":error-code, "java.sun.com/xml/ns/javaee":exception-type} 是预期的。
It is correct that avoidance of mapping specific error code with error page has started with servlet 3.0 specification so I got it resolved with the following change: http://java.sun.com/xml/ns/javaee/web-app_3_0.xsdversion=3.0
从 servlet 3.0 规范开始避免将特定错误代码与错误页面映射是正确的,所以我通过以下更改解决了这个问题:http: //java.sun.com/xml/ns/javaee/web-app_3_0.xsd版本=3.0
Look at the version changefrom anything to 3.0
看版本从什么到3.0的变化