Skip to Main Content
Java Enterprise Best Practices
book

Java Enterprise Best Practices

by O'Reilly Java Authors
December 2002
Intermediate to advanced content levelIntermediate to advanced
288 pages
9h 46m
English
O'Reilly Media, Inc.
Content preview from Java Enterprise Best Practices

Use Common Error Pages for the Whole Application

The JSP page directive’s errorPage attribute can be used to specify a page to be invoked when an error occurs while processing the page. The page can be a static HTML file, a servlet, or a JSP page. A JSP page used as an error page (designated as such by the page directive’s isErrorPage attribute) has access to a Throwable instance that represents the error through the implicit exception scripting variable, or through the EL expression ${pageContext.exception}.

While this mechanism might be suitable for a small application using only JSP pages, it’s not appropriate for an enterprise application using a combination of servlets, filters, and JSP pages. Using a shared error handler for all these component types lets you better control error handling and fine-tune it in one place when needed. To use a global error handler, do not use the errorPage attribute in the JSP pages. Instead, declare an error handler in the web application deployment descriptor (the web.xml file):

<web-app>
   . . . 
  <servlet>
    <servlet-name>errorHandler</servlet-name>
    <servlet-class>
      com.ora.jsp.servlets.ErrorHandlerServlet
    </servlet-class>
    <init-param>
      <param-name>errorPage</param-name>
      <param-value>/shared/error.html</param-value>
    </init-param>
  </servlet>
   . . . 
  <error-page>
    <exception-type>java.lang.Throwable</exception-type>
    <location>/errorHandler</location>
  </error-page>
   . . . 
</web-app>

Here, I define a servlet as the error handler for all types of exceptions; ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Moving to Java 9: Better Design and Simpler Code

Moving to Java 9: Better Design and Simpler Code

Trisha Gee
Java EE 8 High Performance

Java EE 8 High Performance

Romain Manni-Bucau

Publisher Resources

ISBN: 0596003846Supplemental ContentErrata Page