Stateful Session Bean

EJB stateful session bean in java with an example for beginners and professionals. there are 5 types of animation used in stateful session bean: @stateful, @postconstruct, @predestroy, @prepassivate, @postactivate. Let see the example of Stateful Session Bean, Create a stateful bean component, Create stateful bean client, etc.

Stateful Session Bean

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, the conversational state between multiple method calls is maintained by the container in the 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 a stateful session bean application, we are going to use the Eclipse IDE and glassfish 3 servers.

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

1) Create a stateful bean component

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

File: BankRemote.java

 

  1. import javax.ejb.Remote;  
  2. @Remote  
  3. public interface BankRemote {  
  4.     boolean withdraw(int amount);  
  5.     void deposit(int amount);  
  6.     int getBalance();  
  7. }  

File: Bank.java

 

  1. import javax.ejb.Stateful;  
  2. @Stateful(mappedName = "stateful123")  
  3. public class Bank implements BankRemote {  
  4.     private int amount=0;  
  5.     public boolean withdraw(int amount){  
  6.         if(amount<=this.amount){  
  7.             this.amount-=amount;  
  8.             return true;  
  9.         }else{  
  10.             return false;  
  11.         }  
  12.     }  
  13.     public void deposit(int amount){  
  14.         this.amount+=amount;  
  15.     }  
  16.     public int getBalance(){  
  17.         return amount;  
  18.     }  
  19. }  

2) Create a stateful bean client

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

File: index.jsp

 

  1. <a href="OpenAccount">Open Account</a>  

File: operation.jsp

 

  1. <form action="operationprocess.jsp">  
  2. Enter Amount:<input type="text" name="amount"/><br>  
  3.   
  4. Choose Operation:  
  5. Deposit<input type="radio" name="operation" value="deposit"/>  
  6. Withdraw<input type="radio" name="operation" value="withdraw"/>  
  7. Check balance<input type="radio" name="operation" value="checkbalance"/>  
  8. <br>  
  9. <input type="submit" value="submit">  
  10. </form>  

File: operationprocess.jsp

 

  1. <%@ page import="com.samtpoint.*" %>  
  2. <%  
  3. BankRemote remote=(BankRemote)session.getAttribute("remote");  
  4. String operation=request.getParameter("operation");  
  5. String amount=request.getParameter("amount");  
  6.   
  7. if(operation!=null){  
  8.       
  9.     if(operation.equals("deposit")){  
  10.         remote.deposit(Integer.parseInt(amount));  
  11.         out.print("Amount successfully deposited!");  
  12.     }else  
  13.         if(operation.equals("withdraw")){  
  14.             boolean status=remote.withdraw(Integer.parseInt(amount));  
  15.             if(status){  
  16.                 out.print("Amount successfully withdrawn!");  
  17.             }else{  
  18.                 out.println("Enter less amount");  
  19.             }  
  20.         }else{  
  21.             out.println("Current Amount: "+remote.getBalance());  
  22.         }  
  23. }  
  24. %>  
  25. <hr/>  
  26. <jsp:include page="operation.jsp"></jsp:include>  

File: OpenAccount.java

 

  1. import java.io.IOException;  
  2. import javax.ejb.EJB;  
  3. import javax.naming.InitialContext;  
  4. import javax.servlet.ServletException;  
  5. import javax.servlet.annotation.WebServlet;  
  6. import javax.servlet.http.HttpServlet;  
  7. import javax.servlet.http.HttpServletRequest;  
  8. import javax.servlet.http.HttpServletResponse;  
  9. @WebServlet("/OpenAccount")  
  10. public class OpenAccount extends HttpServlet {  
  11.     //@EJB(mappedName="stateful123")  
  12.     //BankRemote b;  
  13.     protected void doGet(HttpServletRequest request, HttpServletResponse response)  
  14.                 throws ServletException, IOException {  
  15.         try{  
  16.             InitialContext context=new InitialContext();  
  17.             BankRemote b=(BankRemote)context.lookup("stateful123");  
  18.               
  19.             request.getSession().setAttribute("remote",b);  
  20.             request.getRequestDispatcher("/operation.jsp").forward(request, response);  
  21.           
  22.         }catch(Exception e){System.out.println(e);}  
  23.     }  
  24.