O'Reilly logo

Head First Servlets and JSP, 2nd Edition by Bert Bates, Bryan Basham, Kathy Sierra

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Configuring error pages in the DD

Sure, you want to be friendly when the user doesn’t know the exact resource to ask for when they get to your site or web app, so you specify default/welcome files. But you also want to be friendly whenthings go wrong. We already looked at this in the chapter on Using Custom Tags, so this is just a review.

Declaring a catch-all error page

This applies to everything in your web app—not just JSPs.

<error-page>
  <exception-type>java.lang.Throwable</exception-type>
  <location>/errorPage.jsp</location>
</error-page>

(FYI: you can override this in individual JSPs by adding a page directive with an errorPage attribute.)

Declaring an error page for a more explicit exception

This configures an error page that’s called only when there’s an ArithmeticException. If you have both this declaration and the catch-all above, then any exception other than ArithmeticException will still end up at the “errorPage.jsp”.

<error-page>
  <exception-type>java.lang.ArithmeticException</exception-type>
  <location>/arithmeticError.jsp</location>
</error-page>

Declaring an error page based on an HTTP status code

This configures an error page that’s called only when the status code for the response is “404” (file not found).

<error-page>
  <error-code>404</error-code>
  <location>/notFoundError.jsp</location>
</error-page>

Note

You can’t use <error-code> and <exception-type> together!

You can configure an error page based on the HTTP status code OR based on the exception type thrown, but you CANNOT ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required