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"
             key="" />

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:
  • 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.

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;

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{
     EmpidSearchForm bsf=(EmpidSearchForm)af;
     String empid=bsf.getEmpid();
          ArrayList<Employee>al=new ArrayList<Employee>();
          Employee Emp=new Employee(empid,"Arbind","IT","","1000000");
                   req.setAttribute("EMPINFO", al); 
          throw new EmpIDNotFoundException();
     return am.findForward("empidsearch");

package com.javaforecast4u.struts;

public class EmpIDNotFoundException extends Exception {


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){
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) { = email;
public String getPhone() {
     return phone;
public void setPhone(String phone) { = phone;


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


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "">


<form-bean name="EmpidSearchForm" type="com.javaforecast4u.struts.EmpidSearchForm"/>

<action path="/EmpidSubmit" name="EmpidSearchForm"
<exception type="com.javaforecast4u.struts.EmpIDNotFoundException"
             key="errors.empidsearch.empid.notfound" />

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


<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="" prefix="jlc" %>
<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>
<logic:present name="EMPINFO" scope="request">
<table border="1">
<tr><td>Employee ID</td>
<td>Employee Name</td>
<jlc:forEach var="EMP" items="${EMPINFO}">
<td>${EMP.empid }</td>
<td>${EMP.ename }</td>
<td>${EMP.dept }</td>
<td>${ }</td>
<td>${ }</td>


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="" xmlns:xsi="" xsi:schemaLocation="">

  <param-value>/WEB-INF/struts-config.xml </param-value>
I hope you will be enjoyed with this tutorial.


Post a Comment