Tomcat:自定义错误页面不起作用

时间:2012-07-25 19:48:35

标签: java eclipse tomcat web.xml

这应该很简单。我修改了项目的web.xml,以便显示我自己的500的错误页面:

<error-page>
    <error-code>500</error-code>
    <location>/error.jsp</location>
</error-page>

然后我编写了最简单的servlet来测试它:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
    response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}

然后完全忽略自定义页面(位于根目录下),而是获取默认的Tomcat错误页面。

我正在使用MyEclipse调试器附带的Tomcat 6版本。我已经三倍检查了这个错字,但我找不到任何异常。我想知道 error-page 标签是否需要位于文档的某个部分。有谁知道会出现什么问题?

*编辑: 这是请求的完整web.xml:

 <?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 

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

<display-name></display-name>

<jsp-config> 
   <jsp-property-group> 
      <url-pattern>/js/base.js</url-pattern>
      <url-pattern>/js/catalog.base.js</url-pattern>
      <url-pattern>/js/minicatalog.base.js</url-pattern>
      <url-pattern>/js/addresses.js</url-pattern>
   </jsp-property-group> 
</jsp-config>

<mime-mapping>
    <extension>pdf</extension>
    <mime-type>application/pdf</mime-type>
</mime-mapping>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:/applicationContext.xml</param-value>
</context-param>

<!-- error-page>
    <error-code>403</error-code>
    <location>/index.jsp?redirect=1</location>
</error-page-->

<error-page>
    <error-code>500</error-code>
    <location>/error.jsp</location>
</error-page>

<!-- BASE -->
<servlet>
    <description>User entity methods.</description>
    <display-name>User</display-name>
    <servlet-name>UserServlet</servlet-name>
    <servlet-class>com.sitei.base.servlet.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>UserServlet</servlet-name>
    <url-pattern>/servlet/UserServlet</url-pattern>
</servlet-mapping>

<servlet>
    <description>Person entity methods.</description>
    <display-name>Person</display-name>
    <servlet-name>PersonServlet</servlet-name>
    <servlet-class>com.sitei.base.servlet.PersonServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>PersonServlet</servlet-name>
    <url-pattern>/servlet/PersonServlet</url-pattern>
</servlet-mapping>

<servlet>
    <description>Company entity methods.</description>
    <display-name>Company</display-name>
    <servlet-name>CompanyServlet</servlet-name>
    <servlet-class>com.sitei.base.servlet.CompanyServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>CompanyServlet</servlet-name>
    <url-pattern>/servlet/CompanyServlet</url-pattern>
</servlet-mapping>

<servlet>
    <description>Module entity methods.</description>
    <display-name>Module</display-name>
    <servlet-name>ModuleServlet</servlet-name>
    <servlet-class>com.sitei.base.servlet.ModuleServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>ModuleServlet</servlet-name>
    <url-pattern>/servlet/ModuleServlet</url-pattern>
</servlet-mapping>

<servlet>
    <description>Profile entity methods.</description>
    <display-name>Profile</display-name>
    <servlet-name>ProfileServlet</servlet-name>
    <servlet-class>com.sitei.base.servlet.ProfileServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>ProfileServlet</servlet-name>
    <url-pattern>/servlet/ProfileServlet</url-pattern>
</servlet-mapping>

<servlet>
    <description>Address entity methods.</description>
    <display-name>Address</display-name>
    <servlet-name>AddressServlet</servlet-name>
    <servlet-class>com.sitei.base.servlet.AddressServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>AddressServlet</servlet-name>
    <url-pattern>/servlet/AddressServlet</url-pattern>
</servlet-mapping>
<!-- END BASE -->

<!-- PUSH -->
<servlet>
    <servlet-name>icepush</servlet-name>
    <servlet-class>org.icepush.servlet.ICEpushServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>icepush</servlet-name>
    <url-pattern>*.icepush</url-pattern>
</servlet-mapping>
<!-- END PUSH -->

<!-- TRUCKS -->
<servlet>
    <description>Employee entity methods.</description>
    <display-name>Employee</display-name>
    <servlet-name>EmployeeServlet</servlet-name>
    <servlet-class>com.sitei.trucks.servlet.EmployeeServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>EmployeeServlet</servlet-name>
    <url-pattern>/servlet/EmployeeServlet</url-pattern>
</servlet-mapping>

<servlet>
    <description>Vehicle entity methods.</description>
    <display-name>Vehicle</display-name>
    <servlet-name>VehicleServlet</servlet-name>
    <servlet-class>com.sitei.trucks.servlet.VehicleServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>VehicleServlet</servlet-name>
    <url-pattern>/servlet/VehicleServlet</url-pattern>
</servlet-mapping>

<servlet>
    <description>VehicleBrand entity methods.</description>
    <display-name>VehicleBrand</display-name>
    <servlet-name>VehicleBrandServlet</servlet-name>
    <servlet-class>com.sitei.trucks.servlet.VehicleBrandServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>VehicleBrandServlet</servlet-name>
    <url-pattern>/servlet/VehicleBrandServlet</url-pattern>
</servlet-mapping>

<servlet>
    <description>Report methods.</description>
    <display-name>Report</display-name>
    <servlet-name>ReportServlet</servlet-name>
    <servlet-class>com.sitei.trucks.servlet.ReportServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>ReportServlet</servlet-name>
    <url-pattern>/servlet/ReportServlet</url-pattern>
</servlet-mapping>

<servlet>
    <description>ExpenseLog methods.</description>
    <display-name>ExpenseLog</display-name>
    <servlet-name>ExpenseLogServlet</servlet-name>
    <servlet-class>com.sitei.trucks.servlet.ExpenseLogServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>ExpenseLogServlet</servlet-name>
    <url-pattern>/servlet/ExpenseLogServlet</url-pattern>
</servlet-mapping>

<servlet>
    <description>IncomeLog methods.</description>
    <display-name>IncomeLog</display-name>
    <servlet-name>IncomeLogServlet</servlet-name>
    <servlet-class>com.sitei.trucks.servlet.IncomeLogServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>IncomeLogServlet</servlet-name>
    <url-pattern>/servlet/IncomeLogServlet</url-pattern>
</servlet-mapping>

<servlet>
    <description>FileExport methods.</description>
    <display-name>FileExport</display-name>
    <servlet-name>FileExportServlet</servlet-name>
    <servlet-class>com.sitei.trucks.servlet.FileExportServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>FileExportServlet</servlet-name>
    <url-pattern>/servlet/FileExportServlet</url-pattern>
</servlet-mapping>
<!-- END TRUCKS -->

<session-config>
  <session-timeout>15</session-timeout> 
</session-config>

<!--security-constraint>
    <web-resource-collection>
        <web-resource-name>LOGIN</web-resource-name>
        <url-pattern>/*</url-pattern>
    </web-resource-collection>
    <user-data-constraint>
        <transport-guarantee>CONFIDENTIAL</transport-guarantee>
    </user-data-constraint>
</security-constraint-->

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

3 个答案:

答案 0 :(得分:16)

在servlet中,您应该调用:

response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);

而不是:

response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);

setStatus用于在没有错误时设置返回状态代码(例如, 状态代码SC_OKSC_MOVED_TEMPORARILY)。如果出现错误,则应使用sendError方法。

答案 1 :(得分:2)

response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);

将发送一个http状态为500的响应。这表示内部错误,但不会在您的应用程序中发出错误。

尝试

throw new ServletException();
而是

,您将看到您的配置按预期工作。

答案 2 :(得分:0)

您的web.xml是什么样的?特别是servlet / filter映射?您可能需要将<dispatcher>ERROR</dispatcher>添加到servlet /过滤器映射中。