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 have both in the same <error-page> tag.

Note

You must use the fully-qualified class name in <exception-type>!

Don’t be fooled by something like this:

<exception-type>
     IOException
</exception-type>

You MUST use the fully-qualified class name, and any Throwable is allowed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset