Introduction to Stateful Session Bean In NetBeans

A dark teal gradient background with subtle code-like patterns, evoking an IDE workspace

In the previous blog post Creating J2EE Modules in NetBeans IDE, we covered the basics of creating an EJB Module in NetBeans IDE. Here we will learn to create a stateful session bean using NetBeans IDE.

Step 1: Create the Session Bean

Click on your EJB Module and select New → Session Bean. A configuration dialog will appear.

Step 2: Choose Session Type

Since we are creating a stateful session bean, select Stateful as the session type. In the Create Interface option, you can select the interface type that suits your application — typically Remote if the client runs in a different JVM, or Local for same-JVM access.

Step 3: Add Create Methods

Stateful session beans maintain conversational state with a specific client across multiple method calls. To support this, add create methods to your bean. These methods are invoked by the client to obtain a reference to the bean instance, and they allow the container to initialize the bean's state for that particular client session.

Step 4: Add Business Methods

With the bean created, you can now add business methods that implement your application logic. These methods operate on the conversational state maintained by the bean. Because the bean is stateful, each client gets its own dedicated instance, and the container preserves the bean's state between successive method invocations from the same client.

Once your stateful session bean is complete, deploy the EJB module to your application server. In the next tutorial, we will cover how to call the business methods of this stateful session bean from a Java client class.