Last Updated on 31 July 2019   |   Print Email
Struts provides an easy way for handling uncaught exceptions which might be thrown during execution of action classes. Uncaught exceptions are ones which are not caught by the regular try-catch clause. There are two methods for handing uncaught exceptions in Struts:
Global exception handling: specifies exception mappings (exception type - view name) which apply to all action classes in a Struts package.
Exception handling per action: specifies exception mappings which apply to a specific action class.
Both methods require adding exception mappings in struts.xml configuration file. Let’s go through each method in details.
1. Global exception handling in Struts
Add the following code snippet just after the <package> element in struts.xml file:
The <global-results> element defines global view names. Here the view named “error” is mapped with the actual view page “/Error.jsp”.The <global-exception-mappings> element specifies a set of <exception-mapping> element which maps an exception type to a view name. Here the exception of type java.lang.Exception is mapped to the view name “error”. That means when any uncaught exception of type java.lang.Exception or its sub types is thrown, Struts will redirect users to the view page mapped with the name “error”.In the Error.jsp page we can access information of the exception as follows:
Let’s see a complete example in action. Consider the following Struts action class:
package net.codejava.struts;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
public class SumAction extends ActionSupport implements ServletRequestAware {
private HttpServletRequest request;
private int sum;
public int getSum() {
return sum;
}
public String execute() {
// an exception might be thrown here if x/y is not a number
int x = Integer.parseInt(request.getParameter("x"));
int y = Integer.parseInt(request.getParameter("y"));
sum = x + y;
return SUCCESS;
}
@Override
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
}
The execute() method parses two numbers x and y from the request and produces sum of both. In this case, an exception might be thrown if either x or y is not a number (a java.lang.NumberFormatException exception is raised).Code of the Error.jsp page:
The following screenshot is output of the application when passing two numeric parameters:If either a parameter is not a number, an exception is thrown and our Error.jsp page will be displayed instead:Of course we can specify multiple exception mappings as follows:
The mapping with most specific exception type will take precedence.NOTES: With global exception handling method, the view names specified in the <exception-mapping> elements must be declared in the <global-results> element.
2. Exception handling per action in Struts
This method specifies the <exception-mapping> elements inside the <action> element, for example:
That tells Struts to redirect the users to the view “dbError” when an exception of type java.sql.SQLException (or its sub type) is thrown inside the action class ConnectDBAction.Code of the action class (ConnectDBAction.java) file:
The action method execute() simply tries to connect to a MySQL database. If success, return the view named “success”.Code of the success view (DBConnect.jsp):
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Database Connection Result</title>
</head>
<body>
<center>
<h1>Connected to database successfully!</h1>
</center>
</body>
</html>
Code of the error page (DBError.jsp):
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Database Error page</title>
</head>
<body>
<center>
<h1>Sorry, a database exception occurred:</h1>
<h2>Exception name: ${exception}</h2>
</center>
</body>
</html>
Output when connecting to the database successfully:Output when an exception is thrown (e.g the username/password is incorrect):NOTE: In this exception handling per action method, the view names specified in the <exception-mapping> elements can refer to either views declared in the enclosing action or in the <global-results> element.
3. Logging exceptions in Struts
In addition to redirect the users to a specific error handling page, Struts also allows us to log exceptions in servlet container’s console and/or in log files. To enable logging exceptions, place the following code just after the <package> element:
That declares an interceptor named “appDefaultStack” which extends from the Struts default stack and configures two attributes of the Struts exception interceptor: logEnabled and logLevel. Behind the scene, the Struts exception interceptor looks at the exception mappings, and redirects the users to the appropriate view when an exception is raised. But the exceptions logging is not enabled by default. The logLevel can be one of the following values: trace, debug, info, warn, error and fatal.When enabled, Struts logs the exceptions in servlet container’s console as in the following example:If a file logging is configured (such as log4j), the exceptions are also written into the log files.So far the struts.xml file used in this tutorial’s examples is as follows:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
Comments