You are on page 1of 3

JSP page error handling

Error handling for JSP pages can be performed in various ways:

Error handling from within the page


For JSP pages that require more intricate error handling and recovery, a page
can be written to directly handle errors from the data bean. The JSP page can
either catch exceptions thrown by the data bean or it can check for error
codes set within each data bean, depending on how the data bean was
activated. The JSP page can then take an appropriate recovery action based
on the error received. Note that a JSP page can use any combination of the
following error handling scopes.

Error JSP page at the page level


A JSP page can specify its own default error JSP page from an exception
occurring within it through the JSP error tag. This enables a JSP page to
specify its own handling of an error. A JSP page that does not contain a JSP
error tag will have an error fall through to the application-level error JSP
page. In the page-level error JSP page, it must call the JSP helper class
(com.ibm.server.JSPHelper) to roll back the current transaction.

Error JSP page at the application level


An application under WebSphere can specify a default error JSP page when an
exception from within any of its servlets or JSP pages occurs. The application-
level error JSP page can be used as a mall level or store level (for a single
store model) error handler. In the application-level error JSP page, a call
must be made to the servlet helper class to roll back the current transaction.
This is because the Web controller will not be in the execution path to roll
back the transaction. Whenever possible, you should rely on the preceding
two types of JSP error handling. Use the application-level error handling
strategy only when required.

JSP is very mature about handling errors. JSP use Exception handling classes for
dealing with error. Every program or code can throw an error; to rectify these errors
should know what real problem is. These error classes in JSP tell us what exact
problem is where we have to do modify in our code to remove error. This give us
detail and deep information of error, can be display on particular error page or put it
in error object System.err.
JSP provide try catch block to hand run time exception. JSP have property in

<%@ page isErrorPage="true" %>


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

try catch block is important feature to caught exception in JSP page, do according to programmer specification.

Example of try catch Error Handling in JSP

tryCatch.jsp
<%@ page language="java" errorPage="" %>
<html>
<head>
<title>Error Handling by try catch block in JSP</title>
</head>

<body>
<%
try{
int a=0;
int b=10;
int c=b/a;
}
catch(Exception e)
{
e.printStackTrace(); /// This will give detail information of error
occur
out.print("Error caught by Catch block is : "+e.getMessage());
}
%>
</body>
</html>

Example of Error page in JSP

errorPage.jsp

<%@ page language="java" errorPage="" %>


<html>
<head>
<title>Error Handling by try catch block in JSP</title>
</head>

<body>
<%
try{
int a=0;
int b=10;
int c=b/a;
}
catch(Exception e)
{
e.printStackTrace(); /// This will give detail information of error
occur
out.print("Error caught by Catch block is : "+e.getMessage());
}
%>
</body>
</html>

second page is error page

error.jsp
<html>
<head>
<title>Caught Error in JSP Page</title>
</head>

<body>
Error is found on this page(This is own customized error page)
</body>
</html>

You might also like