May 31, 2011

EJB3 Stateful Session Bean - Hello World Example

0) To setup an EJB3 project within an Enterprise Application (EAR) and deploy it to the JBoss Application Server, visit this link

1) In the MyEJBProject, create two classes - Account.java and AccountBean.java - within the ejbModule folder

Account.java


package info.icontraining.session;


import javax.ejb.*;


@Remote
public interface Account {


  public float deposit(float amount);
  public float withdraw(float amount);
  
  @Remove 
  public void remove();


}


AccountBean.java


package info.icontraining.session;


import javax.ejb.*;


@Stateful(name="AccountBean")
@Remote(Account.class)  
public class AccountBean implements Account {
   
    float balance = 0;


    public float deposit(float amount){
        balance += amount;
        return balance;
    }
 
    public float withdraw(float amount){
        balance -= amount;
        return balance;
    }

    @Remove  
    public void remove() {
        balance = 0;
    }
}


2) Within the web application, create two JSPs - accountForm.jsp and accountDetails.jsp - within the WebContent folder

accountForm.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1" %>


<html>
<body>


<h1><p align="center">Bank Transaction Form</p></h1>
<hr/><br/>

<form action="accountDetails.jsp" method="post">
  <table align="center"> 
   <tr><td>Enter the amount: <input type="text" name="amt" size="10"></td></tr>
   <tr><td><b>Select your choice:</b></td></tr>
   <tr><td><input type="radio" name="operation" value ="dep">Deposit</td></tr>
   <tr><td><input type="radio" name="operation" value ="with">Withdraw<br/></td></tr>
   <tr><td><input type="radio" name="operation" value ="balance">Check Balance<br/></td></tr>
   <tr><td><input type="submit" value="Submit"><input type="reset" value="Reset"></td></tr>
  </table>
</form>


</body>
</html>



accountDetails.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1" %>
<%@ page import="info.icontraining.session.*, javax.naming.*"%>


 <%!
     public Account account = null;
     float bal=0;


     public void jspInit() {
         try {
            InitialContext ic = new InitialContext();
            account = (Account) ic.lookup("MyEarApp/AccountBean/remote");
            System.out.println("Loaded Account Bean");
         } catch (Exception ex) {
            System.out.println("Error: "+ ex.getMessage());
         }
     }
    
     public void jspDestroy() {
        account = null;
     }
%>


   <%
    try {
       String s1 = request.getParameter("amt");
       String s2 = request.getParameter("operation");


       if (s1.equals("")) s1=null;
       
       if ( s1 != null) {
           Float amt  = new Float(s1);
                
           if(s2.equals("dep")) {
               bal=account.deposit(amt.floatValue());
               out.println("Balance is: " + bal);
               out.println("<br/><a href=accountForm.jsp>Go back</a>");


           } else if(s2.equals("with")) {
               bal=account.withdraw(amt.floatValue());
               out.println("Balance is: " + bal);
               out.println("<br/><a href=accountForm.jsp>Go back</a>");
           
           } else {        
   %>
             <p>Please select your choice</p>
                
   <%
           }
    } else {
   
           if (account != null) {  
   %>
           <p><b>Your Current Balance is:</b> <%= bal%><br/>
           <% out.println("<br/><a href=accountForm.jsp>Go back</a>"); %>
           <p>
   <%     
           }
     }
  } catch (Exception e) {
       e.printStackTrace();
  }
%>


3) Deploy the Enterprise Application comprising of the EJB Project and the Web Application.
Type the following URL in the browser and test the stateful session bean:

http://localhost:8080/MyEarApp/accountForm.jsp

No comments:

Post a Comment