Develop > Presentation layer > Work with JSP pages
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 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.
The JSP page should use try and catch blocks to capture the exception so that it can take appropriate action based on the exception type. Here is an example of a JSP snippet using 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 the 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 using 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 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 a page-level error JSP page, it must call a JSP helper class...
com.ibm.commerce.server.JSPHelper...to roll back the current transaction.
Here is an example of including error handing at the page level:
- Create a single error JSP page which 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:
<%@ page isErrorPage="true" %>In the errorHandler JSP page, use ErrorDataBean or StoreErrorDataBean to retrieve more information about the exception and display messages.
- Include the errorHandler JSP page in other JSP pages, by using this JSP directive to specify that if there are exceptions occuring on the current page, forward the request to errorHandler.jsp:
<%@ page errorPage="/errorHandler.jsp" %>
Error handling at the application level
An application under WebSphere 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. In the application-level error JSP page, a call must be made to the servlet helper class to roll back the current transaction. Use the application-level error handling strategy only when required.
Include error handler at the application level using the deployment descriptor (WEB-INF/web.xml) of the Web application. For example, 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>
Use 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 will be 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>
Note that 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.
Related concepts
Related tasks
Define WebSphere Commerce error message resources: message-resources
Related reference
JSP programming best practice: Use the StoreErrorDataBean data bean for error handling