Make your own error pages

The guy surfing your site doesn’t want to see your stack trace. And he’s not too thrilled to get a standard “404 Not Found”, either.

You can’t prevent all errors, of course, but you can at least give the user a friendlier (and more attractive) error response page. You can design a custom page to handle errors, then use the page directive to configure it.

The designated ERROR page (“errorPage.jsp”)

image with no caption

The BAD page that throws an exception (“badPage.jsp”)

image with no caption

What happens when you request “badPage.jsp”

image with no caption

She doesn’t know about the <error-page> DD tag.

image with no caption

You can declare error pages in the DD for the entire web app, and you can even configure different error pages for different exception types, or HTTP error code types (404, 500, etc.).

The Container uses <error-page> configuration in the DD as the default, but if a JSP has an explicit errorPage page directive, the Container uses the directive.

Get Head First Servlets and JSP, 2nd Edition 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.