In this lesson you will learn how to develop Hello World Session Bean. We will use ant to build the application. Our application will be deployed on the Web Logic Application for testing.
What is Session Bean?
Session is one of the EJBs and it represents a single client inside the Application Server. Stateless session is easy to develop and its efficient. As compare to entity beans session beans require few server resources.
A session bean is similar to an interactive session and is not shared; it can have only one client, in the same way that an interactive session can have only one user. A session bean is not persistent and it is destroyed once the session terminates.
Session Bean Types
Session Beans are of two types, Stateful Session Bean and Stateless Session Bean.
Stateless Session Beans
A stateless session bean does not maintain a conversational state for the client. When a client invokes the method of a stateless bean, the bean's instance variables may contain a state, but only for the duration of the invocation.
Because stateless session beans can support multiple clients, they can offer better scalability for applications that require large numbers of clients. Typically, an application requires fewer stateless session beans than stateful session beans to support the same number of clients.
Stateful Session Beans
The state of an object consists of the values of its instance variables. In a stateful session bean, the instance variables represent the state of a unique client-bean session. Because the client interacts ("talks") with its bean, this state is often called the conversational state.
Writing Stateful Session Bean
A Session Bean is composed of the following parts:
Steps involved in developing the Stateful Session Bean can be summarized in following steps:
Define Home Interface
The Session bean's home interface defines one or more create(...) methods and each create method must be named create and must match one of the ejbCreate methods defined in the enterprise Bean class. The return type of a create method is the Bean's remote interface type.
In case of Stateless Session Bean there is one create
method with no arguments. A remote home interface extends the
interface, while a local home interface extends the
Here is the source code of our home interface:
* Home interface for TestSessionBean.
* @author Deepak Kumar
* @Web http://www.roseindia.net
* @Email firstname.lastname@example.org
public interface TestSessionBeanHome
public examples.TestSessionBean create()
Define Remote Interface
As mentioned earlier, Remote Interface is the client view of the bean and the functions defined in the remote interface is visible to the client. There should be an implementation in the Bean class for the all the functions defined in the remote interface.
Here is the source code of our Remote Interface:
Define Enterprise Bean Class
In the EJB class we write all the business methods along with required that is necessary to implement. In the EJB class methods are defined public. The Session Bean interface methods that the EJB provider must must be defined in the Session bean class are:
A stateful session Bean with container-managed transaction demarcation can optionally also implements the javax.ejb.SessionSynchronization interface.
The Session Synchronization interface methods that must be developed are:
Here is the source code of our Session Bean class:
In the next lesson we will write the deployment descriptors and the servlet to test the Hello World Session bean.
If you enjoyed this post then why not add us on Google+? Add us to your Circles
Liked it! Share this Tutorial
Discuss: Writing Session bean - Session Bean Example with Source Code
Post your Comment