7

好吧,这应该很简单。我修改了项目的 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 版本。我已经三倍地检查了这个是否有错别字,但我找不到任何不寻常的地方。我想知道错误页面标签是否需要位于文档的某个部分。有谁知道可能出了什么问题?

*编辑:这是请求的完整 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>

4

3 回答 3

16

在 servlet 中,您应该调用:

response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);

代替:

response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);

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

于 2012-07-26T07:34:34.740 回答
2
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);

将发送一个带有 http 状态 500 的响应。这说明了一个内部错误,但不会在您的应用程序中发出一个。

尝试

throw new ServletException();

相反,您会看到您的配置按预期工作。

于 2012-07-25T22:20:32.083 回答
0

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

于 2012-07-25T20:03:36.497 回答