Struts Exception Handling with example

In struts we can handle the exception using <exception>. We have to use this exception tag in struts-config.xml file. The attributes of exception tag is as follows:


type: type is use for given the full java class name that mean class name with package. This is class a user define class where we define the exception.


path: path is use for display the exception message. It is a jsp page where we can show the exception.


key: key is use for matching the exception message in resource bundle file that is a property file.


Here we see like as 

<exception type="com.javaforecast4u.struts.EmpIDNotFoundException"
            path="/empidsearch.jsp"
             key="errors.bidsearch.bid.notfound" />

Now our requirements are:
  • Develop a employee search form. Search employee bye their employee Id.
  • If employee Id is not found, give an exception message as Employee ID Not Found 
  • If  employee Id found then show the employee details.
Let see the user interface



















After enter wrong Employee ID we get exception error message as like 





















Now we enter the correct Employee ID is E123 then we get fine result























So for the above requirements we have to write following files:
  • EmpidSearchForm.java
  • EmpIdSearchAction.java
  • EmpIDNotFoundException.java
  • Employee.java
  • Application.properties
  • struts-config.xml
  • empidsearch.jsp
  • web.xml
Note: In WEB-INF/lib folder all jar files should be there 
  • commons-beanutils.jar
  • commons-collections.jar
  • commons-digester.jar
  • commons-fileupload.jar (your choice for uploading file)
  • commons-lang.jar
  • commons-logging.jar
  • commons-validator.jar
  • jakarta-oro.jar
  • struts.jar
  • struts-legacy.jar
and Tld files should in WEB-INF folder :
  • struts-bean.tld
  • struts-html.tld
  • struts-logic.tld
  • struts-nested.tld
  • struts-template.tld
  • struts-tiles.tld
Now we write source of all above mentioned files.

EmpidSearchForm.java

package com.javaforecast4u.struts;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.*;

public class EmpidSearchForm extends ActionForm{
     private String empid;

     public String getEmpid() {
          return empid;
     }

     public void setEmpid(String empid) {
          this.empid = empid;
     }
     
}

EmpIdSearchAction.java

package com.javaforecast4u.struts;

import java.util.ArrayList;

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

import org.apache.struts.action.*;

public class EmpIdSearchAction extends Action{
    
public ActionForward execute(ActionMapping am, ActionForm af, HttpServletRequest req, HttpServletResponse res)throws Exception{
     System.out.println("execute()");
    
     EmpidSearchForm bsf=(EmpidSearchForm)af;
    
     String empid=bsf.getEmpid();
     if(empid.equals("E123")){
          ArrayList<Employee>al=new ArrayList<Employee>();
          Employee Emp=new Employee(empid,"Arbind","IT","arbind@gmail.com","1000000");
          al.add(Emp);
                   req.setAttribute("EMPINFO", al); 
     }else{
          throw new EmpIDNotFoundException();
     }
     return am.findForward("empidsearch");
}
}

EmpIDNotFoundException.java

package com.javaforecast4u.struts;

public class EmpIDNotFoundException extends Exception {
    

}

Employee.java

package com.javaforecast4u.struts;

public class Employee {
private String empid;
private String dept;
private String ename;
private String email;
private String phone;
public Employee(){}
public Employee(String empid,String sname,String sid,String email,String phone){
     super();
     this.empid=empid;
     this.dept=sid;
     this.ename=sname;
     this.email=email;
     this.phone=phone;
}
public String getEmpid() {
     return empid;
}
public void setEmpid(String bid) {
     this.empid = bid;
}
public String getDept() {
     return dept;
}
public void setDept(String dept) {
     this.dept = dept;
}
public String getEname() {
     return ename;
}
public void setEname(String sname) {
     this.ename = sname;
}
public String getEmail() {
     return email;
}
public void setEmail(String email) {
     this.email = email;
}
public String getPhone() {
     return phone;
}
public void setPhone(String phone) {
     this.phone = phone;
}

}
 Application.properties

errors.empidsearch.empid.notfound=<font color=red size=5>Employee ID Not Found</font>


struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!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="EmpidSearchForm" type="com.javaforecast4u.struts.EmpidSearchForm"/>
</form-beans>

<action-mappings>
<action path="/EmpidSubmit" name="EmpidSearchForm"
        type="com.javaforecast4u.struts.EmpIdSearchAction"
        scope="request"
        validate="true"
         input="/empidsearch.jsp">
<exception type="com.javaforecast4u.struts.EmpIDNotFoundException"
            path="/empidsearch.jsp"
             key="errors.empidsearch.empid.notfound" />

  <forward name="empidsearch" path="/empidsearch.jsp"/>
  </action>
       
</action-mappings>
<message-resources parameter ="com.javaforecast4u.struts.Application"/> 
</struts-config>

 empidsearch.jsp

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="jlc" %>
<html>
<body bgcolor="yellow" ><br><br>
<h1><font color="green">Welcome to Java Forecast 4u</font> </h1>
<h1>Search Employee Form</h1>
<html:form action="/EmpidSubmit" method="post">
<table style="font-size:25">
<tr><td>Enter Employee ID</td></tr>
<tr><td><html:text property="empid"/></td></tr>
<tr><td><html:errors /></td></tr>
<tr><td><html:submit value="Search"/></td></tr>
</table>
</html:form>
<logic:present name="EMPINFO" scope="request">
<table border="1">
<tr><td>Employee ID</td>
<td>Employee Name</td>
<td>Department</td>
<td>Email</td>
<td>Phone</td>
</tr>
<jlc:forEach var="EMP" items="${EMPINFO}">
<tr>
<td>${EMP.empid }</td>
<td>${EMP.ename }</td>
<td>${EMP.dept }</td>
<td>${EMP.email }</td>
<td>${EMP.phone }</td>
</tr>
</jlc:forEach>
</table>
</logic:present>
</body>
</html>


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

  <display-name>Lab3</display-name>
  <welcome-file-list>
   
    <welcome-file>empidsearch.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
  <servlet-name>action</servlet-name>
  <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
  <init-param>
  <param-name>config</param-name>
  <param-value>/WEB-INF/struts-config.xml </param-value>
  </init-param>
  <init-param>
  <param-name>debug</param-name>
  <param-value>3</param-value>
  </init-param>
  <init-param>
  <param-name>detail</param-name>
  <param-value>3</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  <servlet-name>action</servlet-name>
  <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>
I hope you will be enjoyed with this tutorial.

0 comments :

Post a Comment