Error page in JSP

This tutorial will help you to know about the JSP Error page.

Error page in JSP

Error page in JSP

In this section we will discuss about "Error page in JSP". When you write code sometimes it may happen that programmer leave a coding error which can occur at any time than while executing the program an exception occurs that redirects programmer to error page. When you execute the JSP page a runtime error may occur inside the page or outside the page.

JSP Error page

Error occurred at runtime may be handled by two ways they are as follows:

  • Using standard Java exception handling code, you can catch and handle the exception.
  • The Exception you do not catch in the JSP page will be forwarded to you as the error page.

You can specify the URL of the error page in the directive in the originating JSP page by setting error page parameter like as follows:

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

By above declaration you can understand how you can write the error page parameter. In the error page you have to specify isErrorPage parameter to true. The exception object of java.lang.exception describe the error that is accessible in error page through implicit exception object. The implicit exception object is accessible in the error page only.

Now, here is the example to illustrate the error page in JSP example:

The "ErrorPageDemo.jsp"  generate an error and forward you to "Errorhandling.jsp" which print the content of exception object.

Code for ErrorPage.jsp

<HTML>
<BODY>
<%@ page errorPage="Errorhandling.jsp" %>
Arithmetic Exception Divide by Zero is generated below:
<%
int a=10/0;
%>
</BODY>
</HTML>

Code for Errorhandling.jsp

<HTML>
<BODY>
<%@ page isErrorPage="true" %>
<b>Here is your error:
<%= exception %></b>
</BODY>
</HTML>

When the error occurrs / by zero then "ErrorPage.jsp" will redirect to "Errorhandling.jsp" which will display the exception object message.

Output of the program :

Download Source Code