JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven







Strut2 Exception Handling

Overview

  • Struts 2 provides robust exception handling.
  • Including the ability to automatically log any uncaught exception and redirect the user to a error web page.
Global Exception Exception Handling

  • Using struts2 framework you can specify in the struts.xml how the framework should handle uncaught exception.
  • The handling logic can apply to all action or to apecific action.

How to enable Global Exception Handling

to enable global exception handling you need to add two nodes to struts.xml:
global-exception-mapping and global-result.

<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error">
</global-exception-mappings>

<global-results>
<result name="error">/error.jsp<result>
</global-results>


  • The global exception mapping node tell the struts 2 framework what to do if an caught exception of the type specified is thrown by the application.
  • All the uncaught exception will cause the stuts 2 Action class to return a resuilt of "error".

Exception Handling Per Action

If you need to handle an exception in a specific way for a certain action you can use the exception-mapping node withing the action node.

<action name="actionspecificexception" class ="">

exception-mapping exception="java.lang.Exception"
result="login"/>
<result>/register.jsp</result>
<result name="login">/login.jsp</result>

</action>

Example:-Create a index.jsp file as given below.

<%page language="java" contentType="text/html;
 charset=ISO-8859-1"

    pageEncoding="ISO-8859-1"%>
<%taglib prefix="s" uri="/struts-tags" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
 charset=ISO-8859-1"
>
<title>Insert title here</title>
</head>
<body>
<h1> Exception Demo</h1>

<s:form action="ActionDemo" method="post">

<s:textfield name="num1"/>
<s:textfield name="num2"/>
<s:submit/>
</s:form>

<h1> Exception with Action class</h1>
<a href="ActionDemo2"> Action Exception</a>

</body>
</html>

Create struts.xml file as given below
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD
Struts Configuration 2.0//EN"

    "http://struts.apache.org/dtds/
struts-2.0.dtd"
>

<struts>
  <package name="default" extends="struts-default">

    <interceptors>
      <interceptor-stack name="appDefault">
        <interceptor-ref name="defaultStack">
          <param name="exception.logEnabled">true</param>
          <param name="exception.logLevel">ERROR</param>
        </interceptor-ref>
      </interceptor-stack>
    </interceptors>

    <default-interceptor-ref name="appDefault" />

    <global-results>
      <result name="error">/globalerror.jsp</result>

    </global-results>

    <global-exception-mappings>

      <exception-mapping exception="java.lang.RuntimeException"
        result="error" />
    </global-exception-mappings>




    <action name="ActionDemo" class="myaction.ActionDemo">
      <result name="success">/success.jsp</result>
    </action>

  </package>

</struts>

Create a ActionDemo.java class in myaction package as given below.

package myaction;

import com.opensymphony.xwork2.ActionSupport;

public class ActionDemo extends ActionSupport {

  private String num1;
  private String num2;

  public String getNum1() {
    return num1;
  }

  public void setNum1(String num1) {
    this.num1 = num1;
  }

  public String getNum2() {
    return num2;
  }

  public void setNum2(String num2) {
    this.num2 = num2;
  }

  public int getSum() {
    int sum = Integer.parseInt(num1+ Integer.parseInt(num2);
    return sum;
  }

  @Override
  public String execute() throws Exception {

    getSum();
    return "success";
  }

}

Create a success.jsp file as given below.

<%page language="java" contentType="text/html;
 charset=ISO-8859-1"

  pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"
>
<html>


<body>
  <%
Object obj=request.getAttribute("sum");
if(obj!=null)
out.println(obj.toString());
 %>
  <h1>This is success page</h1>
</body>
</html>
</body>
</html>

Create a globalerror.jsp file as given below.

<%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"
>
<title>Insert title here</title>
</head>
<body>
  <h1>This is global error</h1>
  This is global error
</body>
</html>

Create web.xml file.

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

Directory Structure



Output:- When we deploy the project the output will be as given below.



when user enter valid number it added it and show the sum of given two number in next page success.jsp.



But when user enter invalid number it transfer the request to globalerror.jsp page.







Download Source without lib
Download War with lib