Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tomcat: Custom error page not working

Well this should've been simple enough. I modified the project's web.xml in order to display my own error page for 500's:

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

Then I wrote the simplest servlet in order to test it:

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

The custom page (which is located at the root directory) is then completely ignored and I get the default Tomcat error page instead.

I'm using the Tomcat 6 version that comes with the debugger of MyEclipse. I've tripled-checked this for typos, but I can't find anything unusual. I'm wondering whether the error-page tag needs to be in a certain part of the document. Does anyone know what could be going wrong?

*Edit: This is the full web.xml that was requested:

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

like image 405
JayPea Avatar asked Jul 25 '12 19:07

JayPea


People also ask

How do I create a custom error page for when Tomcat is down for maintenance?

A common requirement is to have Apache display a custom maintenance error page when Tomcat is down or when an application running in Tomcat is down. This is quite easy to do. All we need to do is add an ErrorDocument entry to our http. conf file.

Where is the default error page in Tomcat?

The default Tomcat error page exposes the Tomcat version that SDM is using. After the above changes, when the SDM URL is accessed via http://<SD server hostname>:<port#>/abc, a screen similar to the following will appear that does not have the Tomcat version listed.


2 Answers

In the servlet, you should call:

response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);

Instead of:

response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);

The setStatus is used to set the return status code when there is no error (for example, for the status codes SC_OK or SC_MOVED_TEMPORARILY). If there is an error, the sendError method should be used instead.

like image 196
M. Abbas Avatar answered Oct 20 '22 12:10

M. Abbas


response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);

will send out a reponse with http status 500. This is stating an internal error, but does not issue one in your application.

Try

throw new ServletException();

instead and you'll see your configuration is working as expected.

like image 2
Arne Avatar answered Oct 20 '22 10:10

Arne