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







Struts Exception Handling

Overview

  • Struts provides robust exception handling.
  • Including the ability to automatically log any uncaught exception and redirect
    the user to a error web page.
  • Entries in struts-config.xml (or web.xml) indicate what
    pages should be displayed when various errors occur
    <exception key="some.key"
    type="package.SomeException"
    path="/WEB-INF/somepage.jsp"/>
  • Works well when custom page needed when error occurs
  • The Struts approach applies only to exceptions thrown by Actions

Entries in struts-config.xml
  • exception element with 4 possible attributes
    • key: entry in a resource bundle (properties file) used to build an ActionMessage
      and that can be output with html:errors.
    •  type: the fully qualified class of the exception. The most specific match is used. Applies only to Actions.
    •  path: the address (relative to the Web app) of the error page. Should normally be in WEB-INF.
    •  handler: the class that will handle the error. Usually omitted, and default handler used.
      <exception key="errors.null" type="java.lang.NullPointerException" path="/WEB-INF/errors/null.jsp"/>
  • Used within global-exceptions or action

Three Basic Approaches

  • Give page for specific type of exception
    • Use exception entry in global-exceptions or action
  • Give page for generic Exception
    •  Use exception entry in global-exceptions, or
    •  Use exception-type entry in web.xml, or use error-page attribute of page directive
      •  Advantage of page directive: the exception object is bound, so you can print a backtrace
  • Give custom handler for specific type of exception
    • Use exception entry in global-exceptions or action, and
    • Define subclass of ExceptionHandler
Global Exception Exception Handling
  • Using struts 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
  • global-exceptions>
        <exception key="error1.msg" type="java.lang.RuntimeException"
          path="/GlobalError.jsp" />
      </global-exceptions>
  • To enable global exception handling you need to struts.xml:
    global-exception-mapping tag as shown above.
  • The global exception mapping tag tell the struts  framework what to do if an caught
    exception of the type specified is thrown by the application.
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 tag withing the action node as shown below.


        <action path="/Action2" type="pkg1.ActionDemo2">
        <exception 
        key="error2.msg" 
        type="java.lang.NullPointerException"
        path="/ActionError.jsp"
        />
        <forward name="success" path="/success2.jsp" />
        </action>
      

Example:

index.jsp
<%@taglib uri="http://struts.apache.org/tags-html"  prefix="html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd"
>
<html>
<body>
<h1> Exception Demo</h1>
<form action="Action1.do" method="post">
<input type="text" name="num1">
<input type="text" name="num2">
<input type="submit">
</form>

<br>
<h1> Exception with Action class</h1>
<a href="Action2.do"> Action Exception</a>

</body>
</html>

FormDemo1.java
package pkg1;

import org.apache.struts.action.ActionForm;

public class FormDemo1 extends ActionForm {
public String num1,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 String getSum()
{
  int sum=Integer.parseInt(num1)+Integer.parseInt(num2);
  return ""+sum;
}
}

ActionDemo1.java
package pkg1;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class ActionDemo1 extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws Exception {
  FormDemo1 form1=(FormDemo1)form;
  request.setAttribute("sum",form1.getSum());
  return mapping.findForward("success");
}
}


ActionDemo2.java
package pkg1;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;


public class ActionDemo2 extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws Exception {
  String str=null;
  System.out.println(str.charAt(0));
return mapping.findForward("success");
}
}

struts-config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation/
/DTD Struts Configuration 1.3//EN"

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

<struts-config>

  <form-beans>
    <form-bean name="form1" type="pkg1.FormDemo1"></form-bean>
  </form-beans>

  <global-exceptions>
    <exception key="error1.msg" type="java.lang.RuntimeException"
      path="/GlobalError.jsp" />
  </global-exceptions>


  <action-mappings>
    <action path="/Action1" type="pkg1.ActionDemo1" scope="request"
      validate="false" input="/index.jsp" name="form1">
      <forward name="success" path="/success1.jsp" />
    </action>

    <action path="/Action2" type="pkg1.ActionDemo2">
    
    <exception 
    key="error2.msg" 
    type="java.lang.NullPointerException"
    path="/ActionError.jsp"
    />
    <forward name="success" path="/success2.jsp" />
    </action>
  </action-mappings>

  <message-resources parameter="MessageResources" />

</struts-config>

MessageResources.properties
error1.msg=This is error one
error2.msg=This is error TWO

GlobalError.jsp
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd"
>
<html>
<head>

</head>
<body>
<h1>This is global error</h1>
<html:errors />
</body>
</html>


ActionError.jsp
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

</head>
<body>
<h1>This is Action Error page</h1>
<html:errors />
</body>
</html>

success1.jsp
<html>
<head>

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

success2.jsp

<html>
<head>

</head>
<body>
<h1>This is success two </h1>
</body>
</html>


Output:-
  • When We run index,jsp page


  • when the user enter valid number it added it and show the sum of  the
    numbers in the next page success1.jsp as shown below.





  • When the user enter the invalid number it trasfer the request to GlobalError.jsp
    page as shown below.






  • When the User click on ActionException Link  NullPointerException is thrown
    and it transfer the request to ActionError.jsp as shown below.
  • This is for a particular Action.




Download Eclipse Code without lib

Download code with lib(WAR)