Stateful Session Bean

Stateful Session bean is a business object that represents business logic like stateless session bean. But, it maintains state (data).

In other words, conversational state between multiple method calls is maintained by the container in stateful session bean.

# Annotations used in Stateful Session Bean

There are 5 important annotations used in stateful session bean:

  1. @Stateful
  2. @PostConstruct
  3. @PreDestroy
  4. @PrePassivate
  5. @PostActivate

# Example of Stateful Session Bean

To develop stateful session bean application, we are going to use Eclipse IDE and glassfish 3 server.

As described in the previous example, you need to create bean component and bean client for creating session bean application.

# 1) Create stateful bean component

Let's create a remote interface and a bean class for developing stateful bean component.


package com.syntaxbook;
import javax.ejb.Remote;
public interface BankRemote {
 boolean withdraw(int amount);
 void deposit(int amount);
 int getBalance();

package com.syntaxbook;
import javax.ejb.Stateful;
@Stateful(mappedName = "stateful123")
public class Bank implements BankRemote {
 private int amount=0;
 public boolean withdraw(int amount){
  return true;
  return false;
 public void deposit(int amount){
 public int getBalance(){
 return amount;

# 2) Create stateful bean client

The stateful bean client may be local, remote or webservice client. Here, we are going to create web based client and not using dependency injection.

File: index.jsp

<a href="OpenAccount">Open Account</a>
File: operation.jsp

<form action="operationprocess.jsp">
Enter Amount:<input type="text" name="amount"/><br>

Choose Operation:
Deposit<input type="radio" name="operation" value="deposit"/>
Withdraw<input type="radio" name="operation" value="withdraw"/>
Check balance<input type="radio" name="operation" value="checkbalance"/>
<input type="submit" value="submit">
File: operationprocess.jsp

<%@ page import="com.syntaxbook.*" %>
BankRemote remote=(BankRemote)session.getAttribute("remote");
String operation=request.getParameter("operation");
String amount=request.getParameter("amount");

 out.print("Amount successfully deposited!");
  boolean status=remote.withdraw(Integer.parseInt(amount));
  out.print("Amount successfully withdrawn!");
  out.println("Enter less amount");
  out.println("Current Amount: "+remote.getBalance());
<jsp:include page="operation.jsp"></jsp:include>

package com.syntaxbook;
import javax.ejb.EJB;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class OpenAccount extends HttpServlet {
 //BankRemote b;
 protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
  InitialContext context=new InitialContext();
  BankRemote b=(BankRemote)context.lookup("stateful123");
  request.getRequestDispatcher("/operation.jsp").forward(request, response);
 }catch(Exception e){System.out.println(e);}