Common/ErrorServlet

From tehowiki
Revision as of 13:50, 11 October 2016 by imported>Gfis (Link to Apache Tomcat)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

ErrorServlet handles Http server error codes (e.g. 404 = file not found), and unexpected internal Java exceptions and shows a corresponding message to the user.

There are several conditions which must be met by the web application:

  • The servlet and all methods called therein must not catch any exceptions themselves, but must - possibly after logging - throw them to the caller. In the end, that will be the container (Tomcat) or application server (WebSphere), which activates the ErrorServlet.
  • The configuration file WEB-INF/web.xml should contain an element for at least the Servlet 3.0 specification (December 2009), which is implemented by Tomcat Version 7 and above.
<web-app 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"
        version="3.0">
  ...
 </web-app>
 
  • web.xml must contain proper references to the ErrorServlet:
  
    ...
    <servlet>
        <servlet-name>ErrorServlet</servlet-name>
        <servlet-class>org.teherba.common.web.ErrorServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ErrorServlet</servlet-name>
        <url-pattern>/ErrorServlet</url-pattern>
    </servlet-mapping>

    <error-page>
        <location>/ErrorServlet</location>
    </error-page>
    ...