Handle Exception two ways in Web app
Discussion
In J2ee web application, there are two types of errors in the Web Tier:
HTTP Errors (400 – 500 series response)
Communicate problems to Web clients.-
Java Exceptions
Communicate problems to Web container.
Exceptions can be generated within the tier or passed back from other tiers.
If you want exceptions to be passed on to a client, you must convert them to HTTP errors.
Exceptions thrown to the client
- javax.servlet.ServletException
- javax.io.IOException
In J2ee Web application, javax.servlet.ServletException and java.io.IOException will be thrown to the client.
All the exceptions will be wrapped into ServletException and thrown to the client, if you do not handle them by yourself.
So, we decide handling exceptions two ways:
- Handle Exceptions which are thrown under Action tier using ExceptionHandler in Struts (see post: Exception Handling in Struts)
- Handle Exceptions which are thrown over Action tier by configurating web.xml using tag <error-page>
approach 2:
In web.xml
<error-page>
<exception-type>javax.servlet.ServletException</exception-type>
<location>/error.do</location>
</error-page>
<error-page>
<exception-type>javax.io.IOException</exception-type>
<location>/error.do</location>
</error-page>
Define ErrorAction to handle exceptions
public class ErrorAction extends Action {
/**
* The request attribute under which we
* forward an HTTP status message
* (as an object of type STring) to an error page.
*/
public static final String ERROR_MESSAGE_ATTR =
"javax.servlet.error.message";
/**
* The request attribute under which we
* forward a Java exception type
* (as an object of type Class) to an error page.
*/
public static final String EXCEPTION_TYPE_ATTR =
"javax.servlet.error.exception_type";
/**
* The request attribute under which we
* forward the request URI
* (as an object of type String) of the page on which
* an error occurred.
*/
public static final String EXCEPTION_PAGE_ATTR =
"javax.servlet.error.request_uri";
/**
* The request attribute under which we
* forward a Java exception
* (as an object of type Throwable) to an error page.
*/
public static final String EXCEPTION_ATTR =
"javax.servlet.error.exception";
/**
* The request attribute under which we
* forward an HTTP status code
* (as an object of type Integer) to an error page.
*/
public static final String STATUS_CODE_ATTR =
"javax.servlet.error.status_code";
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
// ErrorMessage (java.lang.String)
Object errorMsg =
request.getAttribute(ERROR_MESSAGE_ATTR);
// Exception (java.lang.Exception)
Object exception =
request.getAttribute(EXCEPTION_ATTR);
// Exception Class (java.lang.Class)
Object exceptionType =
request.getAttribute(EXCEPTION_TYPE_ATTR);
// uri (java.lang.String)
Object page =
request.getAttribute(EXCEPTION_PAGE_ATTR);
// Error Code (java.lang.Integer)
Object errorCode =
request.getAttribute(STATUS_CODE_ATTR);
ActionErrors errors =
(ActionErrors) request.getAttribute(
Globals.ERROR_KEY);
if(errors == null) {
errors = new ActionErrors();
}
errors.add("innerError",
new ActionMessage("errors.inner.error",
errorMsg, exception, errorCode, page));
saveErrors(request, errors);
return mapping.findForward("toErrorPage");
}
}
In struts-config.xml, define ErrorAction
<action
path="/error"
type="org.springframework.web.struts.DelegatingActionProxy"
scope="request">
<forward name="toErrorPage" path="/error_page.jsp"/>
</action>
MessageResources.properties:
errors.inner.error=Error Message: {0}
<br />Error Type : {1} <br />Error Code : {2}
<br />Request URI : {3}
Jsp File
<span class=”errorMsg”><html:errors /></span>
Doing so, and handling exception in struts using ExceptionHandler,
you can handle the exceptions thrown not only under Action tier, but also over the Action tier.