Exception handling in struts

Here I am explaining how to handle Exceptions in struts .

struts-config.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>

<form-beans>
<form-bean name="exceptionform" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="id" type="java.lang.String"/>
</form-bean>
</form-beans>

<global-exceptions>
<exception
key ="errors.exception1"
type="com.Exception.Exception1"
handler="com.Exception.MyHandler"
path="/Except.jsp"/>
<exception
key ="errors.exception2"
type="com.Exception.Exception2"
handler="com.Exception.MyHandler"
path="/Except.jsp"/>
</global-exceptions>

<action-mappings>

<action path="/exceptiontest"
name="exceptionform"
scope="request"
input="/ExceptionTest.jsp"
validate="true"
type="com.Exception.ExceptionTest">
<forward name="success" path="/ExceptionTest.jsp"/>
<forward name="failure" path="/Except.jsp"/>
</action>

</action-mappings>

<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>

</struts-config>

web.xml

<web-app>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>ExceptionTest.jsp</welcome-file>
</welcome-file-list>

<taglib>
<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
</web-app>

Exception1.java

package com.Exception;

public class Exception1 extends Exception {

public Exception1() {
super();
}

public Exception1(String message) {
super(message);
}

public Exception1(String message, Throwable cause) {
super(message, cause);
}

public Exception1(Throwable cause) {
super(cause);
}

}

Exception2.java

package com.Exception;

public class Exception2 extends Exception {

public Exception2() {
super();
}

public Exception2(String message) {
super(message);
}

public Exception2(String message, Throwable cause) {
super(message, cause);
}

public Exception2(Throwable cause) {
super(cause);
}

}

ExceptionTest.java

package com.Exception;

import java.io.IOException;

import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.validator.*;

public class ExceptionTest extends Action {

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

DynaValidatorForm f = (DynaValidatorForm) form;
String id = (String) f.get("id");

if (id.equals("Exception"))
throw new Exception("Test Exception");

if (id.equals("RuntimeException"))
throw new RuntimeException("RuntimeException");

if (id.equals("Exception1"))
try {
throw new Exception1("Test Exception 1");
} catch (Exception1 e) {
return (mapping.findForward("failure"));
}

if (id.equals("Exception2"))
try {
throw new Exception2("Test Exception 2");
} catch (Exception2 e) {
return (mapping.findForward("failure"));
}

return (mapping.findForward("success"));
}

}

Myhandler.java


package com.Exception;

import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.config.*;

public class MyHandler extends ExceptionHandler {

public ActionForward execute(Exception ex,
ExceptionConfig ae,
ActionMapping mapping,
ActionForm formInstance,
HttpServletRequest request,
HttpServletResponse response)
throws ServletException {

ex.printStackTrace();

return new ActionForward(ae.getPath());
}
}

ExceptionTest.jsp

<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<html>
<body>

<html:form action="exceptiontest.do">
<br>
<br><center>
<h3><u>Form</u></h3><br>
Give Id as Exception or RuntimeException or Exception1 or Exception2 and check the Exception.<br><br>
Enter Id <html:text property="id"/>
<br><br>
<html:submit value="Submit"/>
</center>
</html:form>

</body>
</html>

Except.jsp

<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="org.apache.struts.*" %>

<%

Enumeration paramNames = request.getAttributeNames();
while (paramNames.hasMoreElements()) {
String name = (String) paramNames.nextElement();
Object values = request.getAttribute(name);
out.println("<br> " + name + ":" + values);
}

paramNames = session.getAttributeNames();
while (paramNames.hasMoreElements()) {
String name = (String) paramNames.nextElement();
Object values = session.getAttribute(name);
out.println("<br> " + name + ":" + values);
}

Object o = request.getAttribute("MYEXCEPTION");
if (o == null)
o = request.getAttribute(Globals.EXCEPTION_KEY);
if (o != null) {
Throwable t = (Throwable)o;
out.println("<h3>An Exception was thrown:</h3>" + t);

StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
pw.flush();
String trace = sw.toString();
out.println("<pre>"+trace+"</pre>");
}

%>

No comments: