Using Standard Web Application Error Handling

A JSF application is also a servlet-based application, so truly exceptional runtime exceptions can be trapped and handled by error handlers declared in the web.xml file:

<web-xml>
  ...
  <error-page>
    <exception-type>java.lang.Throwable</exception-type>
    <location>/errorpage.jsp</location>
  </error-page>
  ...
</web-xml>

The <error-page> element contains an <exception-type> element that names an exception type and a <location> element with a context-relative path for the resource to invoke if the exception is thrown by any request. The resource can be a JSP page or servlet that displays a friendly message instead of the stack trace most web containers show by default. It can also log information about the error, available as request scope attributes as well as through the implicit pageContext variable in a JSP page. If you need to deal with different types of exceptions in different ways, you can declare more than one error handler. The web container picks the one with an exception type that most closely matches the thrown exception. For more on all of this, I recommend my book JavaServer Pages (O’Reilly).

Get JavaServer Faces now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.