【问题标题】:Custom Error Page in Tomcat 7 for Error Code 500错误代码 500 的 Tomcat 7 中的自定义错误页面
【发布时间】:2013-04-05 21:59:49
【问题描述】:

伙计们,我正在努力解决在 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。但是错误页面是空的。

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

【问题讨论】:

  • 为什么发生错误时 Web 服务会尝试提供 HTML 页面?
  • ...您的 servlet 和 JSP 与 Web 服务有什么关系?你确定你知道什么是网络服务?
  • 因为我们在项目中使用了自定义错误页面
  • 是的,我知道 servlet 使用什么 web 服务将所有输出发送到浏览器以下载图像和 jsp 页面/html 页面我想清楚地显示我的自定义错误页面?
  • @SumitSharma 我遇到了与您描述的问题类似的问题 - Tomcat 正在打印与我在web.xml 中指定的错误页面不同的错误页面。你解决问题了吗?

标签: java tomcat servlets


【解决方案1】:

尝试将以下 sn-p 放入您的 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 目录处于同一级别(不在其中)。

【讨论】:

  • 这似乎对我有帮助!需要注意的是,顺序很重要,要在 Exception 映射之前进行 500 映射(如图所示)
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2011-02-28
    • 2015-08-26
    • 1970-01-01
    • 2015-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-18
    • 1970-01-01
    相关资源
    最近更新 更多