Tomcat: Custom error page not working

Well, that should have been simple enough. I modified the web.xml project to display my own 500 error page:

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

Then I wrote a simple servlet to test it:

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

The user page (which is in the root directory) is then completely ignored, and instead I get the default Tomcat error page.

I am using the version of Tomcat 6 that comes with the MyEclipse debugger. I tripled - checked it for typos, but I can not find anything unusual. I am wondering if the page tag with the error should be in a specific part of the document. Does anyone know what could go wrong?

* Editing: This is the complete web.xml request:

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

+6
source share
3 answers
 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); 

will send a response with an http status of 500. This indicates an internal error, but does not return it in your application.

Try

 throw new ServletException(); 

and you will see that your configuration is working properly.

+2
source

In the servlet you should call:

 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); 

Instead:

 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); 

SetStatus is used to set the return status code when there is no error (for example, for status codes SC_OK or SC_MOVED_TEMPORARILY ). If there is an error, use the sendError method sendError .

+15
source

What does your web.xml look like? In particular, displaying servlets / filters? You may need to add <dispatcher>ERROR</dispatcher> to your servlet / filter mapping.

0
source

Source: https://habr.com/ru/post/921343/


All Articles