Handling JSP Errors

0
600

To handle the JSP errors, lets have a look of the different types of errors first. There are two types of errors in JSP:

1) Translation Time Errors and 2) Request Time Errors

jsp errors img

Translation Time Errors
These errors occur when a JSP is first requested and goes through the initial translation from a JSP source file into a corresponding Servlet class file. (Read more about JSP Life Cycle in this post)

They are reported to the requested client with an error status code 500 or ‘server error’. They usually contain the reported compilation error. These errors are handled by the JSP Engine.

Request Time Errors
These errors are runtime errors that can occur in either the body of the JSP page or in some other object that is called from the body of the JSP page.

These result in an exception being thrown. These exceptions can be caught and approximately handled in the body of the calling JSP, which would be the end of the error.

Those exceptions not caught result in the forwarding of the client request, including uncaught exception, to the error page specified by the offending JSP.

Handling The Errors

To handle the errors in jsp, we need to create a JSP error page and use it in the JSP. So lets see how can we do that:

Creating a JSP Error Page:

For creating an error page, create a basic JSP page and then you need to tell the JSP engine that the page is an Error Page. Simple by setting its page attribute “isErrorPage” to true.

errorPage.jsp

<%@ page isErrorPage="true" %>

error: <%=exception.getMessage()%> has been reported.

 

Using a JSP Error Page

In the JSP, add 1 additional attribute, in your page directive, to make JSP aware of an error page. Add ‘errorpage’ attribute and set its value equal to the location of your JSP error page.

<%@ page errorPage=errorPage.jsp %>
<%
  if(true) {
    throw new Exception(PURE JSP EXCEPTION);
  }
%>
Previous articleJSP Introduction and Life Cycle
Next articleArmstrong Number
I have spent almost 10 years playing around Java and related technologies. I love to write on different topics and would be very much willing to hear back your feedback/suggestions on them. This site is a medium to share my knowledge with the Java folks and grow further. My other interests include traveling, driving, swimming and dance. But yes, my web site has become my passion over the time :) I live in Scotland and travel to India often, my roots being there.

NO COMMENTS

LEAVE A REPLY