How to define Error page in Java Web Application - Servlet JSP Example (original) (raw)

There are two ways to define the Error page in Java web application written using Servlet and JSP. The first way is page wise error page which is defined on each jsp page and if there is any unhandled exception thrown from that page, the corresponding error page will be displayed. The second approach is an application-wide general or default error page which is shown if any Exception is thrown from any Servlet or JSP and there is no page-specific error page defined.

how do make error page in servlet jsp web application javaIn this java tutorial we will see both approaches to declare error page in JSP and when should we use page specific error page and when should we choose to generate a default application-wide error page in Java web application.

This is in continuation of my earlier post on Servlet and JSP like the top 10 Servlet interview questions and the top 10 JSP interview questions. If you have' read them yet, you can also read them to learn about essential Servlet and JSP concepts.

Page-Specific Error page in JSP

Every JSP page has an attribute called "errorpage" on page directive, by using this attribute you can define an error page for any particular JSP. After that, if any unhandled Exception is thrown from that JSP, this error page will be invoked. In order to make any JSP page as an error page, you need to use "isErrorPage" attribute of the page directive and mark it true.

For example, in the below JSP pages, error.jsp is an error page that can be used to display custom error messages if any unhandled exception is thrown from login.jsp (because it is defined as error page for login.jsp)

<%@ page isErrorPage="true"%>

<%@ page errorPage="error.jsp"%>

This is a preferred way of showing error messages in Java web applications if you have custom error messages based on JSP and it also supersedes any application-wide error page defined in web.xml. You can also join these Servlet and JSP courses to learn more about page directives in JSP.

Error page in Java Web Application JSP Servlet

Application wide Error page in Java web application

There is another way to define error pages in java web applications written using servlet and JSP. This is called an application-wide error page or default/general error page because it's applicable to the whole web application instead of any particular servlet or JSP. Its recommended practice for every Java web application to have a default error page in addition to page-specific error pages.

This error page is defined in web.xml by using tag <error-page>. <error-page> allows you to define custom error messages based upon HTTP error code or any Java Exception. you can define a default error message for all exceptions by specifying <exception-type> as java.lang.Throwable and it would be applicable to all exceptions are thrown from any Servlet or JSP from web application. here is an example of declaring default error page in Java web application based on HTTP Error code and Java Exception type.

Default Error page based on Exception type:

java.lang.Throwable /error.htm

Default Error page based on HTTP Error code:

500 /internal-server-error.htm 404 /page-not-found-error.htm

That's all on how to define the custom error page in Java application both page-specific and an application-wide default error page. An important point to note is that page-specific error pages take precedence over application-wide default error pages defined in web.xml.

Other Java posts you may like