Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

exception interceptor


In our web application, there might occur exception at any point.

To overcome this problem, struts 2 provides a mechanism of global exception handling where we can display a global result to the user.

Struts 2 automatically log the uncaught exceptions and redirects the user to the error handler page.


Understanding the internal working of exception interceptor

If there occurs exception, it is wrapped in ExceptionHolder and pushed in the valuestack so that we can easily access exception object from the result.

It is recommended to make this interceptor as the first interceptor, so that it can handle all the exception whether it is thrown by other interceptors.


Parameters of exception interceptor

There are 3 parameters defined for exception interceptor. All are optional.

Parameter Description
logEnabled specifies log should be enabled or not. You can pass true or false.
logLevel specifies the log level. It may be trace, debug, info, warn, error, fatal. Default log level is debug.
logCategory specifies the log category eg. com.mycompany.app. The default is com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor..

Example of exception handling in struts 2

For exception handling, we specify the global-result and global-exception-mappings in the struts.xml file.

struts.xml

  1. <?xml version=“1.0” encoding=“UTF-8” ?>
  2. <!DOCTYPE struts PUBLIC “-//Apache Software Foundation//DTD Struts
  3.  Configuration 2.1//EN” “http://struts.apache.org/dtds/struts-2.1.dtd”>
  4. <struts>
  5. <package name=“aa” extends=“struts-default”>
  6. <global-results>
  7. <result name=“myresult”>globalresult.jsp</result>
  8. </global-results>
  9. <global-exception-mappings>
  10. <exception-mapping result=“myresult” exception=“java.lang.Exception”></exception-mapping>
  11. </global-exception-mappings>
  12. <action name=“login” class=“com.Login”>
  13. <result>welcome.jsp</result>
  14. <result name=“error”>error.jsp</result>
  15. </action>
  16. </package>
  17. </struts>

The global-results sub-element of package specifies the global-result for this package.

The result sub-element of global-result specifies the result page that will be rendered to the user as a view.

The global-exception-mappings sub-element of package specifies the exception mapping for all the actions of this package.

The exception-mapping sub-element of global-exception-mapping maps the given result for the given exception type. In this example, we are using the Exception which the parent of many exception classes such as IOException, ArithmeticException etc. It means if any exception occurs, specified result will be invoked.

Notice that global-results must be specified before global-exception-mappings as we are using global result in global-exception-mappings.


Displaying exception

We can display the exception on the browser by printing the exception or exceptionStack.

The exception object prints the exception name whereas exceptionStack prints the exception details.

globalresult.jsp

  1. <p>Exception Name: <s:property value=“exception” /> </p>
  2. <p>Exception Details: <s:property value=“exceptionStack” /></p>

Full example of exception handling

The other required resources to complete this example are as follows:

  1. Input page (index.jsp)
  2. Action class (Login.java)
  3. View components (globalresult.jsp, welcome.jsp and error.jsp)

1) Create index.jsp for input

This jsp page creates a form using struts UI tags. It receives name and password from the user.

index.jsp

  1. <%@ taglib uri=“/struts-tags” prefix=“s” %>
  2. <s:form action=“login”>
  3. <s:textfield name=“name” label=“Name”></s:textfield>
  4. <s:password name=“password” label=“Password”></s:password>
  5. <s:submit value=“login”></s:submit>
  6. </s:form>

2) Create the action class

This action class contains two fields name and password and one method execute. Here, we are throwing exception self if password matches struts.

This is dummy example, if you comment the mentioned line in the execute method, exception will not occur.

Login.java

  1. package com;
  2. public class Login {
  3. private String name,password;
  4. //getters and setters
  5. public String execute(){
  6.     if(password.equals(“struts”)){
  7.         int a=12/0;//If you comment this, exception will not occur
  8.         return “success”;
  9.     }else
  10.         return “error”;
  11. }
  12. }

3) Create view components

There are three view components globalresult.jsp that displays the exception message, welcome.jsp that displays the welcome message with the username and error.jsp that displays the error message.

globalresult.jsp

  1. <%@ taglib uri=“/struts-tags” prefix=“s” %>
  2. Sorry an exception occured!
  3. <p>Exception Name: <s:property value=“exception” /> </p>
  4. <p>Exception Details: <s:property value=“exceptionStack” /></p>
  5. <form>
  6. <input type=“button” value=“back” onclick=“history.back()”>
  7. </form>

welcome.jsp

  1. <%@ taglib uri=“/struts-tags” prefix=“s” %>
  2. Welcome, <s:property value=“name”/>

error.jsp

  1. Sorry username or password error!
  2. <jsp:include page=“index.jsp”></jsp:include>

Comments

comments