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

Sum*_*rma 15 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>
Run Code Online (Sandbox Code Playgroud)


我从我的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>
Run Code Online (Sandbox Code Playgroud)


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

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

Har*_*are 27

尝试将以下代码段放入您的 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>
Run Code Online (Sandbox Code Playgroud)

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


小智 5

如果您使用嵌入式Tomcat这样的示例,则可以通过编程实现相同的目的:

        Context appContext = ...            
        ErrorPage errorPage = new ErrorPage();
        errorPage.setErrorCode(HttpServletResponse.SC_NOT_FOUND);
        errorPage.setLocation("/my_custom_error_page.html");
        appContext.addErrorPage(er);
Run Code Online (Sandbox Code Playgroud)