Struts – <global-exception> custom exception handler example

Download this <global-exception> example – Struts-Global-Exception-Example.zip

In Struts framework, the <global-exception> is used to display your custom error page, instead of the default classic HTTP Status 500 error page :

struts-global-exception-1

The default error page look ugly and not professional at all. In addition, the error message is descriptive enough to leak your system information to end user.

1. <global-exception> + default exception handler

Here’s a <global-exception> with default exception handler example declared in “struts.config.xml” to replace the default HTTP 500 error page with your custom descriptive error page.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
 
<struts-config>
 
	<form-beans>
		//...
	</form-beans>
 
        <global-exceptions>
	    <exception
	      key="error.global.mesage"
	      type="java.io.IOException"
	      path="/pages/error.jsp" />
	</global-exceptions>
	
	<action-mappings>
		//...
	</action-mappings>
 
</struts-config>

In above, the default Struts exception handler “org.apache.struts.action.ExceptionHandler” will be called when any IOException is thrown by an Action, and forward it to error.jsp file. The key is a key in your message resources properties file.

Common.properties


#common module error message
error.global.mesage = 
   Ooooppps... Sometime wraong in this site, please come back later

error.jsp

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>





Struts Global Exception Example

2. <global-exception> + custom exception handler

In the default exception handler, you have no way to control how to deal with the exception. In most cases, you may need to log the exception for further analysis. To do this, you need a custom exception handler to log all the exceptions to another data store like file system or database.

Here’s a custom exception handler example to configure for “java.lang.Exception” so that it’s called for any exception thrown by Action. To create a custom exception handler, you need to subclass “org.apache.struts.action.ExceptionHandler” and override the execute method.

MyCustomExceptionHandler.java


package com.mkyong.common.exception;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ExceptionHandler;
import org.apache.struts.config.ExceptionConfig;
 
public class MyCustomExceptionHandler extends ExceptionHandler{
 
  private static final Logger logger = 
      Logger.getLogger(MyCustomExceptionHandler.class);
	
  @Override
  public ActionForward execute(Exception ex, ExceptionConfig ae,
	ActionMapping mapping, ActionForm formInstance,
	HttpServletRequest request, HttpServletResponse response)
	throws ServletException {
		
	//log the error message
	logger.error(ex);
		
	return super.execute(ex, ae, mapping, formInstance, request, response);
  }
}

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
 
<struts-config>
 
	<form-beans>
		//...
	</form-beans>
 
        <global-exceptions>
	    <exception
	      key="error.global.mesage"
	      type="java.io.IOException"
	      handler="com.mkyong.common.exception.MyCustomExceptionHandler"
	      path="/pages/error.jsp" />
	</global-exceptions>
	
	<action-mappings>
		//...
	</action-mappings>
 
</struts-config>

In above, when exception is thrown by Action, it will call your custom exception handler MyCustomExceptionHandler’s execute() method instead of the default exception handler.

Reference

  1. Struts Exception handler – http://struts.apache.org/1.x/userGuide/building_controller.html

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Kumar Shorav
Guest
Kumar Shorav

Sorry , but i donot know how to get the exception message .

Kumar Shorav
Guest
Kumar Shorav

Hi, suppose I am writing some code in action class within Try and Catch block. If any exception occurs it will go to Catch block. Then How to handle such exception. One way I thought that I will use findForward()and forward to specific error page and use the message.

Charles Morin
Guest
Charles Morin

Hi mkyong,

Thank you for this great tutorial.

Is it possible to print the stacktrace in the JSP page? I would like to put the stacktrace into an hidden field, and add a “Submit to developer” button that would send an email to us with the detailed stack trace.

Thank you in advance for help

Charles

Diego Ramos
Guest
Diego Ramos

Use the directive isErrorPage in the jsp that gives you access to the implicit objet pageContext.exception

trackback
Struts Tutorials | Tutorials

[…] <global-exception> custom exception handler Struts <global-exception> to display the custom error page to user. […]