Creating Stateful Session Beans
If Stateless Session Beans do not maintain state, it is easy to guess what Stateful Session
Beans do. Yes, they maintain the state.
When a client invokes a method in a stateful bean, the variables (state) of that request are
kept in the memory by the bean. When more requests come in, the container makes sure that
the same bean is used for the same client. This type of bean is useful when multiple requests
are required and several steps are necessary for completing a task.
Stateful Beans also enjoy the ease of development introduced by Java EE 6, meaning that
they can be created by annotating a POJO with @Stateful.
It is not in the scope of this recipe to learn how Stateful Beans work in detail. If you wish
to learn more, please visit:
http://jcp.org/en/jsr/detail?id=318
Or
https://www.packtpub.com/developer-guide-for-ejb3/book
In this recipe, we will see how to use NetBeans to create a stateful session bean that holds a
counter of how many times a request for a method was executed.
Getting ready
Please find the software requirements and configuration instructions for this recipe in the first
Getting ready section of this chapter.
This recipe builds on the sources of the previou s recipes.
How to do it…
- Right-click on the EJBApplication node and select New Session Bean….
- For Name and Location: Name the EJB as CounterManufacturerEJB.
- Under Package, enter beans.
- Mark Session Type as Stateful.
- Leave Create Interface with nothing marked and click Finish.

Creating the business method
With CounterManufacturerEJB open, add the following variable:
private int counter = 0;
Then right-click inside the class body and select Insert Code… (or press Alt+Insert) and select
Add Business Method….
When the Add Business Method… window opens:
- Name it as counter and for Return Type, enter String.
- Click OK.

counter++; return ""+counter;
Save the file.
Open ManufacturerServlet and after the class declaration and before the
processRequest method:
- Right-click and select Insert Code… or press Alt+Insert.
- Select Call Enterprise Bean….
- In the Call Enterprise Bean window, expand the EJB Application node.
- Select CounterManufacturerEJB and click OK.

@EJB CounterManufacturerEJB counterManufacturerEJB;
Resolve the import errors by pressing Ctrl+Shift+I.
Then add to the process request:
out.println("<b>Number of times counter was accessed<b> " + counterManufacturerEJB.counter() + "<br><br>" );
Save the file.
How it works…
NetBeans presents the user with a very easy-to-use wizard for creating beans. As with the
stateless bean, we are presented with the different options for creating a bean.
This time we select the Stateful Bean. When clicking Finish, the IDE will create the EJB POJO
class, place it in the beans package, and annotate, with @Stateful, the class signifying that
we have created a Stateful Session Bean.
We then proceed to add the logic in our EJB. Through another wizard, NetBeans makes it
easy to add a business method. After pressing Alt+Insert, we are presented with the choices
of what can be done in that context. After adding the code, we are ready to integrate our EJB
with the servlet.
Again, pressing Alt+Insert comes in handy when we want to create a reference to our EJB. After
the correct bean is selected in the Call Enterprise Bean window, NetBeans creates the code:
CounterManufacturerEJB counterManufacturerEJB = lookupCounterManufacturerEJBBean();
And also:
private CounterManufacturerEJB lookupCounterManufacturerEJBBean() { try { Context c = new InitialContext(); return (CounterManufacturerEJB) c.lookup("java:global/ EJBApplication/CounterManufacturerEJB!beans.CounterManufacturerEJB"); } catch (NamingException ne) { Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne); throw new RuntimeException(ne); } }
This boatload of code is created by the IDE and enables the developer to fine-tune things like
logging over exceptions and other customizations. In fact, this is the way that EJB was called
prior to annotations being introduced to Java EE. The method is simply calling the application
server context with the lookup method, along with the Remote Method Invocation (RMI)
naming conventions used to define our EJB and assign the reference to the object itself.
Notice that all this code could be simplified to:
@EJB CounterManufacturerEJB counterManufacturerEJB;
But we tried to show how much liberty and options the developer has in NetBeans.
There’s more…
Disabling GlassFish alive sessions.
GlassFish and sessions
To keep sessions alive in our Application Server GlassFish, we need to navigate to the
Services window:
- There we will need to expand the Servers node.
- Right-click on GlassFish and select Properties.
- Click on Preserve Sessions Across Redeployment if you do not want this feature.
This option preserves the HTTP sessions even when GlassFish has been redeployed. If the
data has been stored in a session, it will be available next time a redeployment occurs.






Pingback: JavaPins