JSP page error handling
Error handling for JSP pages can be performed in various ways.
Error handling within the current 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; that is, handling errors from within the same page. The JSP page can either catch exceptions that are thrown by the data bean or it can check for error codes that are set within each data bean, depending on how the data bean was activated. The JSP page can then take an appropriate recovery action that is based on the error received. A JSP page can use any combination of the following error handling scopes.
The JSP page can use try and catch blocks to capture the exception so that it can take appropriate action that is based on the exception type. Here is an example of a JSP snippet that uses try and catch Java statements:
SomeDataBean sdb = new SomeDataBean();
sdb.setSomeProperty("");
try {
com.ibm.commerce.beans.DataBeanManager.activate(sdb, request); }
catch(Exception ex){
//Handle the exception in whichever way you want..
}
To obtain more details about an error, include an ErrorDataBean in your JSP file. ErrorDataBeans can be instantiated, populated, and used. Here is an example:
ErrorDataBean errorBean = new ErrorDataBean ( );
com.ibm.commerce.beans.DataBeanManager.activate (errorBean, request);
Once the data bean is instantiated, it has methods to get ExceptionData, ExceptionMessage, MessageKey, ExceptionType, and other helper methods.
For storefront JSP files, use StoreErrorDataBean to handle the errors.
Error handling outside of the current page
You can have a separate dedicated error handling page, delegating the request to that page, when an exception occurs in the current JSP page. When you use a separate error handling page, you can have two options: error handling at the page level or at the application level.
Error handling at the page level
A JSP page can specify its own default error JSP page from an exception that is 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 has an error fall through to the application-level error JSP page.
Here is an example of including error handling at the page level:
- Create a single error JSP page that handles the errors that occur across all the other JSP pages
in the application. To specify a JSP page as an errorHandler page, use this JSP page
directive:
In the errorHandler JSP page, use ErrorDataBean or StoreErrorDataBean to retrieve more information about the exception and display messages.<%@ page isErrorPage="true" %>
- Include the errorHandler JSP page in other JSP pages, by using this JSP directive to specify
that if exceptions occur on the current page, forward the request to
errorHandler.jsp:
<%@ page errorPage="/errorHandler.jsp" %>
Error handling at the application level
An application under HCL Commerce 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 site level or store level error handler.
Use the application-level error handling strategy only when required.
Include error handler at the application level by using the deployment descriptor (WEB-INF/web.xml) of the Web application. For example, if you want to handle a PaymentException generically at the application level, code a paymentError.jsp file and specify the same in the web.xml file by using the <error-page> tag. Here is an example:
<error-page>
<exception-type>PaymentException</exception-type>
<location>/paymentError.jsp</location>
</error-page>
Using this example, if PaymentException is thrown in a JSP page that does not handle certain exception types, or if the JSP page does not specify its own error page, then the request is redirected to paymentError.jsp. The paymentError.jsp file can use ErrorDataBean or StoreErrorDataBean to retrieve more information about the exception.
To include a generic page for all exceptions, at application level, use the following in the web.xml file:
<error-page>
<exception-type>Exception</exception-type>
<location>/paymentError.jsp</location>
</error-page>
In
the <exception-type>
tag, instead of using the exception class name, you can
specify the specific error code, such as browser error 404, which is a page not found
error.