Struts integration with EJB in WEBLOGIC7


Struts integration with EJB in WEBLOGIC7

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

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

Struts integration with EJB in WEBLOGIC7


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

Published in developerIQ.


My aim is to write about EJB2.0 in WEBLOGIC-7 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… However, BEA has the longest record and used by many enterprises and hence a standard..Therefore, I am now writing about WebLogic-7 and later, I will cover JBoss3.2 in a separate lesson.( The latest version of Weblogic is WL-9).

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 stateless 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 struts-blank.war. 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 context. 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 weblogic7 and JDK1.4.2 for developing this exercise


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

We have installed weblogic7 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 D:\bea\user_projects\mydomain\applications

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\bea\user_projects\mydomain\applications\kala\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:\strutsejbdemo\demo1;d:\strutsejbdemo;


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:\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 weblogic-jar.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 weblogic-jar.xml

<?xml version="1.0"?>

<!DOCTYPE weblogic-ejb-jar PUBLIC

"-//BEA Systems, Inc.//DTD WebLogic 7.0.0 EJB//EN"









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 check up the jar file as follows

D:\strutsejbdemo>java weblogic.ejbc city.jar city1.jar

If there are no problems , we get the “ejbc successful” message

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

D:\strutsejbdemo>copy city1.jar d:\bea\user_projects\mydomain\applications

Start the weblogic server as start->programfiles->BEA weblogic->user_projects->mydomain->start server

The city1.jar will be deployed by the server. The development of the EJB and its deployment in WebLogic 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();



String url="t3://";


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:\bea\user_projects\mydomain\applications\kala


D:\strutsejbdemo\demo>copy *.class d:\bea\user_projects\mydomain\applications\kala\web-inf\classes\demo

The deployment process is over

Go to another window. cd to D:\bea\user_projects\mydomain\applications\kala\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"






<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 weblogic server as usual.

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

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

text box & then click ‘submit’. If the name of the state 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.

In another article soon, I will extend this concept to connect to Hibernate from the Stateless ejb.


The following articles from back issues of DeveloperIQ.

1) Packaged EJB by Farihah Noushene.

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