Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Struts integration with EJB in JBOSS3.2

My aim is to write about EJB2.0 in JBOSS3.2 using STRUTS FRAMEWORK. The EJB specification provides Enterprise-level services. Typical services provided by the EJB Container are as follows.


Struts 2 Tutorial and Examples Struts General Availability - released Login form in Struts2 version 2.3.16 Struts 2 Login Application Struts 2 version released Struts 2 version released Struts 2 Hello World Annotation Example Struts Roseindia Struts 2 Tutorial: Easy steps to learn Struts 2 Difference between Struts and Spring Apache Struts launched to fix vulnerable Feature Struts2 Application in Eclipse: Running the application in Eclipse IDE Struts 2 Hello World Application using Eclipse Interceptors in Struts 2 Struts 2 Action Tag Struts 2 in Agile Development Environment Struts 2 UI Tags Generic Tags Creating own Interceptor What are Interceptors in Struts 2 and how do they work? Results Value Stack / OGNL Action in Struts 2 Framework Struts 2 Interceptors Struts Maven - How to use Maven dependency of Struts Download Struts How Struts 2 Framework works? Architecture of Struts 2 Framework Features and Enhancements in Struts Struts 2 Tutorials - Struts version Struts 1 Tutorial and example programs Struts Tutorial What is Struts Struts Flow Diagram Step By Step Struts Tutorial Struts 2 Login Form Example Struts Video Tutorials Struts 2 Hello World Example Introduction to Struts 2 Framework Downloading and Installing Struts 2.3.8 In Struts What is Model? What is Struts? Download Struts What is Struts Framework? Struts Framework Latest Version of Struts Framework Downloading Struts Official example Struts 2.3.8 released with new features Struts 2.3.8 Tutorials and Examples Hibernate4 Struts Integration

Struts integration with EJB in JBOSS3.2


by JeyaPuvana,M.C.A(Final year).

My aim is to write about EJB2.0 in JBOSS3.2 using STRUTS FRAMEWORK.

The EJB specification provides Enterprise-level services. Typical services provided by the EJB Container are as follows.


2.Declarative transactions


4.Connection pooling

5.Support for messaging services

The most famous EJB-Servers in the market are:

  • BEA ..WebLogic Server
  • JBoss Application server
  • IBM … .WebSphere-5
  • Oracle AS

Of these , JBoss 3.2 & 4 are OpenSource and Free.

JBOSS application sever has become recognized leader in the java application server market and to date is the major application server.

Different kinds of Enterprise Beans are

  1. Session Bean
  2. Entity Bean
  3. MessageDriven Bean (MDB)

Session Bean

Session Bean can be defined as function bean called in RMI-IIOP style from Enterprise container. Two types of session bean are 
session bean, stateful session bean.

A stateless session bean does not maintain conversation state but stateful session bean maintains conversation state

Entity Bean

Entity Bean is a value bean with setter and getter methods representing one row of data in a table, invoked in RMI style from Enterprise
 container in Enterprise data store. Two types of Entity Beans are CMP (Container Managed Persistence), BMP
 (Bean Managed Persistence). In CMP, persistence code need not be written by the programmer. But In BMP Persistence code has to be 
written by the programmer .CMP is not satisfactory for really complex enterprise situations. ( CMP is an ORM technology 
( Object-Relational Mapping Technology … like Hibernate. and Hibernate is considered to be better than CMP).

Message Driven Bean

Session Bean and Entity bean are based on RPC mechanism. But,

Message Driven Bean is based on Asynchronous messaging . MDB have no conversational state with the client and can be pooled like 
stateless session bean. Unlike other types of EJB, MDB have no home or remote interfaces.

Messaging is like email whereas RPC is like phone call. In enterprise environment there will be heavy traffic and RPC may fail to create 
connection. In such situations messaging is more reliable.

What is Struts?

Struts is a very famous ready made framework which implements MVC (Model View Controller) Architecture. It is open source and free. 
Struts frame work was developed by Craig MacLanahan in 2002 subsequently handed over to Apache Jakarta projects group. Struts is a 
frame work for building really complex Enterprise level applications

Struts provide its own Controller component and integrates with other technologies to provide the Model and the View. For the Model, 
Struts can interact with standard data access technologies, like JDBC and EJB, as well as most any third-party packages, like Hibernate, 
iBATIS, or Object Relational Bridge. For the View, Struts works well with JavaServer Pages, including JSTL and JSF, as well as Velocity 
Templates, XSLT, and other presentation systems

Purpose of the Struts

When the user fills the form and submits the form, if the values entered by the user are wrong then the user must be presented with the same
page with suitable error messages and the values already entered by the user should be preserved.

If there is no error, ( validation test is passed), then the business logic is executed..


Struts1.1 is zip file. Extract it. In Struts1.1 folder, there is a folder name called lib. There are many war files. One of the war file names, is 
. Copy that struts-blank.war file to c:\tomcat5\webapps. Start the tomcat server. Now struts-blank folder is created .

Copy that struts-blank folder and paste it in desktop. Rename the folder as ‘ kala ’ ..

Now, place that folder( ‘ kala ’ ) in c:\tomcat5\webapps.( Assuming that we have installed tomcat5 in C drive). Here ‘ kala ’ folder is our 
. Restart the tomcat server.. Open the Browser, type the URL as ‘ http://localhost:8080/kala. Now the ‘ welcome ’ page will 
appear. This means that our installation process is correct.

We require jboss3.2 and JDK1.4.2 for developing this exercise

The following lesson illustrates the method of connecting to a stateless session bean in jboss3.2 server through a Struts-based program.

We have installed jboss3.2 and JDK1.4.2 in D:\drive . Create a folder ‘ strutsejbdemo ’ in D drive. This is our working directory.

We have already created the context ‘ kala ’ in tomcat5 as mentioned above. .

Copy that ‘ kala ’ context folder to E:\jboss32\server\default\deploy as kala.war

In this lesson, we have created two packages namely demo and demo1. For this we have created the two folders namely demo and 
demo1 in D:\jboss32\server\default\deploy\kala.war\web-inf\classes to copy the class files

D:\>cd strutsejbdemo


When we use struts the concept of package is very important.

D:\strutsejbdemo>md demo1

Here demo1 is our package

D:\strutsejbdemo>cd demo1

Next we create a batch file to set path and classpath

D:\strutsejbdemo\demo1>edit setpath.bat

set path=c:\windows\system32;d:\jdk1.4.2\bin;d:\bea\weblogic700\server\bin

( I am using WindowsXP. If you are using Windows-2000, the path to edit program should be : c:\winnt\system32.)

Usually we give a class path to working directory. When we use package we giveclasspath to parent directory. This is very important.

D:\strutsejbdemo\demo1\edit setcpath.bat

set classpath=d:\ejbdemo;d:\jboss32\client\Log4j.jar;d:\jboss32\client\jboss-j2ee.jar;d:\jboss32\client\jboss-common-client.jar;d:\jboss32\client

After creating batch files in the working folder,

Give the following command:


This will set the path for the window


( This will set the classpath for the window ).

Checkup as follows:


D:\strutsejbdemo\demo1>echo %classpath%

Now edit the following files



package demo1;

import java.util.*;

public class cityguide


public String listcities(String a)


Vector vector1=new Vector();



















String r="";

int n=vector1.size();

for(int j=0;j<n;j++)


String v=(String)vector1.elementAt(j);



return r;





package demo1;

import javax.ejb.*;

import java.rmi.*;

public interface cityRemote extends EJBObject


public String showcities(String s)

throws RemoteException;




package demo1;

import javax.ejb.*;

import java.rmi.*;

public interface cityHome extends EJBHome


public cityRemote create()

throws CreateException, RemoteException;




package demo1;

import javax.ejb.*;

public class cityBean implements SessionBean


public String showcities(String s)


cityguide guide1= new cityguide();

String r = guide1.listcities(s);

return r;


public void ejbCreate() {}

public void ejbRemove() {}

public void ejbActivate() {}

public void ejbPassivate() {}

public void setSessionContext(SessionContext sc) {}



We have already set the specified path and classpath. Compile all the above files

D:\strutsejbdemo\demo1>javac *.java

D:\ejbstrutsdemo\demo1>copy *.class d:\jboss32\server\default\deploy\kala.war\web-inf\classes\demo1


D:\strutsejbdemo>md META-INF

When we use package META-INF folder is created in parent folder. It should be in uppercase ie.,META-INF not meta-inf

Now edit the ejb-jar.xml and jboss.xml in META-INF

D:\strutsejbdemo\META-INF>edit ejb-jar.xml

<?xml version="1.0"?>


"-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN"















D:\strutsejbdemo\META-INF >edit jboss.xml

<?xml version="1.0" encoding="UTF-8"?>










Now, cd back to D:\strutsejbdemo\

Create the jar file as follows:

D:\strutsejbdemo\>jar cf city.jar demo1\*.class META-INF\*.xml

Carefully note that the jar file is created in parent directory.

Now, we copy the jar file to the jboss folder as shown below.

D:\strutsejbdemo> copy city.jar D:\jboss32\server\default\deploy

Start the jboss server as shown below

Go to new window to start the jboss server.

D:\jboss32\bin\>set JAVA_HOME=D:\JDK1.4.2


The server will start now. If our jar file namely city.jar is deployed correctly deployed city.jar message will appear in the server.

The development of the EJB and its deployment in JBOSS server is over.

//----------------------------------------------------------------------------------------------------------Now we develop the Struts client program for the above EJB.

D:\ejbstrutsdemo>md demo


D:\strutsejbdemo\demo>edit setcpath.bat

set classpath=%classpath%;d:\strutsejbdemo;e:\struts1.1\lib\struts.jar;


We use the following six files, in this demo package,

1. kalaSubmit.jsp

2. from ActionForm)

3. from Action)

4. ( a utility bean)

5. ( a value bean)

6. kalaOutput.jsp

( besides the web.xml ,struts-config.xml files & file)

D:\ejbstrutsdemo\demo1>edit kalaSubmit.jsp

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>


<body bgcolor=cyan>





<html:form action="kala"


type="demo.kalaForm" >

State <br>

<h1>Whick State Do you want to tour?</h1>


<html:text property="state" />


<html:submit />





When the user submits the kalaSubmit.jsp, the formbean is automatically filled up with the values from the jsp-page and the flow goes to the ActionServlet.

<html:errors/> tag is used to displaying error messages.


package demo;

import javax.servlet.http.*;

import org.apache.struts.action.*;

public class kalaForm extends ActionForm


String state =null;

public String getState()


return state;


public void setState(String a)




public void reset(ActionMapping mapping,

HttpServletRequest request)




public ActionErrors validate (ActionMapping mapping, HttpServletRequest request)


ActionErrors errors = new ActionErrors();

if( state.length()= =0)


errors.add("state", new ActionError("errors.nullstate"));


return errors;





package demo;

import java.util.*;

import javax.ejb.*;

import javax.rmi.*;

import javax.naming.*;

import java.rmi.*;


public class kalahelper


public String callejb(String s)


String r;



System.out.println("Please Wait...");

Properties props=new Properties();






Context context=new InitialContext(props);

System.out.println("Connection OK");

demo1.cityHome home=


System.out.println("HOME LOCATED");

demo1.cityRemote remote= home.create();

System.out.println("REMOTE READY");



catch(Exception e1){ r=""+e1; }

return r;





package demo;

public class kalaresult


String value;

public kalaresult()


value=" ";


public String getValue(){

return value;


public void setValue(String v){





The Action Class is part of the Model and is a wrapper around the business logic. The purpose of Action Class is to translate the HttpServletRequest to the business logic

In our case, the action class instance is using a helper to perform the business logic. This is the bettter and recommended practice.


package demo;


import java.util.*;

import javax.servlet.*;

import javax.servlet.http.*;

import org.apache.struts.action.*;

public class kalaAction extends Action


public ActionForward execute(ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response)

throws IOException, ServletException


kalaForm kalaform =(kalaForm) form;

String a = kalaform.getState();

if(a.equals("TAMILNADU") || a.equals("KERALA") || a.equals("KARNATAKA"))


kalahelper helper = new kalahelper();

String r = helper.callejb(a);

kalaresult kr = new kalaresult();


String m = kr.getValue();


HttpSession session= request.getSession();


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




ActionErrors errors = new ActionErrors();

errors.add (ActionErrors.GLOBAL_ERROR, new ActionError("errors.wrongname"));







D:\strutsejbdemo\demo>edit kalaOutput.jsp



<body bgcolor=orange>


<jsp:useBean id="result" scope="session" class="demo.kalaresult" />


<jsp:getProperty name="result" property="value" />




Now we are ready to compile the above programs.class path are already specified

D:\strutsejbdemo\demo>javac *.java

D:\strutsejbdemo\demo>copy *.jsp


D:\strutsejbdemo\demo>copy *.class


The deployment process is over

Go to another window. cd to

D:\jboss32\server\default\deploy\kala.war\web-inf>edit struts-config.xml

In the struts-config.xml file, we make two entries. One entry is for the instance of kalaForm and the other entry is for the instance of kalaAction class.


( this is the part dealing with the formbean)



type="demo.kalaForm" />

The next segment of mapping in

struts-config.xml deals with the action mapping for QueryAction.

<action path="/kala"





validate= ” true ” >

<forward name="success"


<forward name="failure"



 There is a file known as '' in 'WEB-INF\classes\resources' and at the end of that file we will add the following lines.

errors.nullstate=<li><h1> ENTER YOUR STATE<h1></li>

errors.wrongname=<li><h1>SORRY … .., SERVICE NOT PROVIDED FOR THIS STATE</h1> </li>

Therefore, we will get the specified error messages.

Start the jboss server as usual.

Open the browser and type URL as http://localhost:8080/kala/kalaSubmit.jsp

Type the name of the state ( TAMILNADU/KERALA/KARNATAKA)in the

text box & then click 'submit '. It the name of the sate is correct then result will be displayed. Otherwise the home page remains with the values entered and also with the error message. As the main purpose of this note is just to explain how to connect to stateless ejb using Struts, I am leaving out Validation Framework details.


The following articles from back issues of DeveloperIQ.

1) Packaged EJB by Farihah Noushene.

2) Developing EJB in Weblogic 7 by R.S.Ramaswamy




Share on Google+Share on Google+

Struts integration with EJB in JBOSS3.2

Posted on: March 11, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Struts integration with EJB in JBOSS3.2   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image