Tomcat 7中的自定义错误页面,用于错误代码500

时间:2013-04-13 11:19:27

标签: java tomcat servlets

伙计我正在努力解决在Windows环境中在Tomcat中打开自定义错误页面的问题。我得到的一些解决方案但没有运气。我尝试了几乎所有的链接,但它不适合我。其中一些用于部署在Tomcat中的其他解决方案,但不适用于我的Web服务

我的 web.xml

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<error-page>
    <location>/error.html</location>
</error-page>


我从我的Servlet 3.0应用程序发送错误 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);

我已将 error.html 放在我的WebService的根目录中。

我为JSP页面获得的一些链接而不是HTML,我也尝试了

对于我使用的JSP:

<%@ page isErrorPage="true" %>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <h1>Error</h1>
</body>
</html>


仅供参考,它适用于其他Tomcat内置解决方案,它运行良好。但是在我的 Webservice 中,我收到了响应500,但页面空白。另一件事我禁用了我的Internet Explorer 9.0显示错误友好页面仍然响应来自servlet 500。但错误页面将变空。

如果对我的查询有任何想法或疑问,请告诉我。我需要尽快。

2 个答案:

答案 0 :(得分:25)

尝试将以下代码段放在WEB-INF/web.xml

<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>

在这种情况下,Error.jspWEB-INF目录处于同一级别(不在其中)。

答案 1 :(得分:4)

如果您使用嵌入式Tomcat,可以通过编程方式实现,例如:

        Context appContext = ...            
        ErrorPage errorPage = new ErrorPage();
        errorPage.setErrorCode(HttpServletResponse.SC_NOT_FOUND);
        errorPage.setLocation("/my_custom_error_page.html");
        appContext.addErrorPage(er);
相关问题