Simple Session Bean EJB 2 0
![]() |
| Moving Forward |
In this blog, We will learn how to create a simple "Hello World" Stateless Session Bean.
![]() |
| A Brief Idea |
A Session Bean is composed of the following parts:
- Home Interface : Clients use the home interface to create, remove, and find Enterprise JavaBeans instances.
- Component Interface : The component interface exposes an Enterprise Java Bean�s business methods to a client. The client calls the methods defined in the component interface to invoke the business logic implemented by session bean.
- Session Bean : The Session bean class is where you implement the business logic defined in the component interface. Session beans implement the javax.ejb.SessionBean interface.
- Deployment Descriptor : A deployment descriptor is an XML-based text file whose elements describe how to assemble and deploy the unit into a specific environment.

According to the EJB 2.0 specification, Component Interface and Home Interface may be either local or remote. Local Interfaces (Home and Component) are to be used by a client running in the same JVM as the EJB component.
We will discuss the �remote� concept later. This is not in the scope of this blog.
![]() |
| Here We Go |
Home Interface : The client uses the Home to obtain instances of the bean. It extends javax.ejb.EJBLocalHome interface and declare create() method.
package example;
import javax.ejb.CreateException;
import javax.ejb.EJBLocalHome;
public interface HelloWorldHome extends EJBLocalHome
{
public HelloWorldLocal create() throws CreateException;
}

The return type of a create method must be the enterprise Beans local interface type. All the exceptions defined in the throws clause of an ejbCreate method must be defined in the throws clause of the matching create method of the home interface.
package example;
import javax.ejb.CreateException;
import javax.ejb.EJBLocalObject;
public interface HelloWorldLocal extends EJBLocalObject
{
public String sayHello() throws CreateException;
}

Here we have declared a method namely sayHello() which will be implemented by our session bean.
Session Bean : The third class for EJBs is the bean implementation class. It implements the functionality provided by the component interface.

package example;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
public class HelloWorldSessionBean implements SessionBean
{
private static final long serialVersionUID = 1L;
// Here is our business logic
public String sayHello()
{
return "EJB Hello, world !";
}
// Methods of Home interface
public void ejbCreate() {}
// Methods of SessionBean interface
// Methods of SessionBean interface
protected SessionContext ctx;
public void setSessionContext(SessionContext ctx)
{
this.ctx = ctx;
}
public void ejbRemove() {}
public void ejbActivate() {}
public void ejbPassivate() {}
}

A stateless session bean needs to have exactly one ejbCreate method with no arguments. It must also define the business methods declared in component interface [sayHello()]
Deployment Descriptor : A deployment descriptor is used to describe and define the EJB bean.
Generally, configuration of EJBs is defined in a file namely ejb-jar.xml and placed within jar under META-INF/ejb-jar.xml.
Deployment Descriptor : A deployment descriptor is used to describe and define the EJB bean.
Generally, configuration of EJBs is defined in a file namely ejb-jar.xml and placed within jar under META-INF/ejb-jar.xml.
For our example, this will be the content of ejb-jar.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar id="ejb-jar_ID" version="2.1" >="http://java.sun.com/xml/ns/j2ee"
>="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd">
<display-name>EJB Demo</display-name>
<enterprise-beans>
<session>
<ejb-name>HelloWorldSessionBean</ejb-name>
<local-home>example.HelloWorldHome</local-home>
<local>example.HelloWorldLocal</local>
<ejb-class>example.HelloWorldSessionBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>HelloWorldSessionBean</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>
There will be one more configuration file for JNDI configuration. In case of Jboss, we use jboss.xml for JNDI configuration. This file should be placed within jar under META-INF/jboss.xml.
Now we will configure JNDI for our sample application in jboss.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS 4.0//EN" "http://www.jboss.org/j2ee/dtd/jboss_4_0.dtd">
<jboss>
<enterprise-beans>
<session>
<ejb-namelink download


