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.

File: BankRemote.java

package com.syntaxbook;
import javax.ejb.Remote;
@Remote
public interface BankRemote {
 boolean withdraw(int amount);
 void deposit(int amount);
 int getBalance();
}
File: Bank.java

package com.syntaxbook;
import javax.ejb.Stateful;
@Stateful(mappedName = "stateful123")
public class Bank implements BankRemote {
 private int amount=0;
 public boolean withdraw(int amount){
 if(amount<=this.amount){
  this.amount-=amount;
  return true;
 }else{
  return false;
 }
 }
 public void deposit(int amount){
 this.amount+=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"/>
<br>
<input type="submit" value="submit">
</form>
File: operationprocess.jsp

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

if(operation!=null){
 
 if(operation.equals("deposit")){
 remote.deposit(Integer.parseInt(amount));
 out.print("Amount successfully deposited!");
 }else
 if(operation.equals("withdraw")){
  boolean status=remote.withdraw(Integer.parseInt(amount));
  if(status){
  out.print("Amount successfully withdrawn!");
  }else{
  out.println("Enter less amount");
  }
 }else{
  out.println("Current Amount: "+remote.getBalance());
 }
}
%>
<hr/>
<jsp:include page="operation.jsp"></jsp:include>
File: OpenAccount.java

package com.syntaxbook;
import java.io.IOException;
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;
@WebServlet("/OpenAccount")
public class OpenAccount extends HttpServlet {
 //@EJB(mappedName="stateful123")
 //BankRemote b;
 protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
 try{
  InitialContext context=new InitialContext();
  BankRemote b=(BankRemote)context.lookup("stateful123");
  
  request.getSession().setAttribute("remote",b);
  request.getRequestDispatcher("/operation.jsp").forward(request, response);
 
 }catch(Exception e){System.out.println(e);}
 }
}