Struts 2 Exception Handling

shape Description

Struts 2 Exception Handling provides, how to handle the uncaught exception in an easy process and redirect users to a dedicated error page. One can easily configure Struts to have different error pages for different exceptions.Struts 2 handle the exceptions very easily, by using ” exception ” interceptor. The “exception” is included as a part of the default stack. Then, one need to configure the structs.xml file in <exception-mapping> tag under the action tag.

shape Example

For example, define one ArithmeticException in an application,

1. Create the project directory structure.

2. Add all the jar files in lib folder, like struts 2 jar files and servlet-api.jar.

3. Create one View page, to get a request from the user.

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!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=ISO-8859-1">
   </head>
   <body>
      <s:form action="exception" >
          <font color="green"><h1>Welcome to SPLessons</h1></font><br>
          <s:textfield name="name" label="Enter your Name" />
          <center><s:submit value="Sign In"></s:submit> </center>
     </s:form>
   </body>
</html>

4. Create the Action class.
MyExceptionAction.java

package com.splessons;

public class MyExceptionAction 
{
  private String name;
  public String getName()
 {
   return name;
  }
  public void setName(String name) 
  {
    this.name = name;
   }
  public String execute()
  {
    int a=5;
    int b=0;
   System.out.println("value of c="+(a/b));
 
    return "success";
   }
}

In this code every thing will be same as previous chapters examples, but the thing is that SPLesson is going to show how exception will be occured, as every one that if any thing by zero obviously it displays exception on the browser as follows in the output.
5. Create the struts.xml and should look like.

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation 
//DTD Struts Configuration 2.1//EN" 
"http://struts.apache.org/dtds/struts-2.1.dtd"> 
<struts>
   <package name="chinna" extends="struts-default"> 
       <action name="exception" class="com.splessons.MyExceptionAction"> 
           <result name="success">/success.jsp</result> 
       </action> 
  </package> 
</struts> 

Where success is a predefined result type. action element is the sub component of package. It speaks to an activity to be conjured for the approaching request. It has name, class and method attributes. In the event that you don’t determine name property as a matter of course execute() technique will be summoned for the predetermined actiion class.
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

6. Run the program on a server, then it display the output as shown below.

7. Enter the required field and click on Sign In button.

By default, the exception interceptor handles exceptions. So, one have to create an error page.
error.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
 <%@ taglib prefix="s" uri="/struts-tags" %>
 
<!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=ISO-8859-1">
   </head>
   <body>
        <font color="blue">Hai<s:property value="name"/>, <h1> This is Your defined custom exception page.</h1></font>
   </body>
</html>

Configure the error page in struts.xml file. When exception is occurred, the error page will be displayed in browser.
struts.xml

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation 
//DTD Struts Configuration 2.1//EN" 
"http://struts.apache.org/dtds/struts-2.1.dtd"> 
<struts>
    <package name="chinna" extends="struts-default"> 
       <action name="exception" class="com.splessons.MyExceptionAction"> 
          <exception-mapping exception="java.lang.ArithmeticException"
 result="error" /> 
          <result name="success">/success.jsp</result>
          <result name="error">/error.jsp</result> 
      </action> 
 </package> 
</struts> 

Here exception mapping element has been placed in the struts.xml file that is java.lang.ArithmeticException.
Output
When recompile the program following is an output will be displayed.

Summary

shape Key Points

  • Struts 2 Exception Handling – Exception interceptor can handle the exception in struts 2.
  • Struts 2 Exception Handling – exception-mapping is the tag will be placed in struts.xml .
  • Struts 2 Exception Handling – error.jsp is the special page will be placed in struts.xml to solve an error.