Java
  Home arrow Java arrow Page 4 - Session Beans in EJB 3.0
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Session Beans in EJB 3.0
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2007-03-28

    Table of Contents:
  • Session Beans in EJB 3.0
  • Stateful Session Beans
  • Implementing Session Beans
  • Session Beans in the Real World

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Session Beans in EJB 3.0 - Session Beans in the Real World


    (Page 4 of 4 )

    In the last part I had developed a Stateless Session Bean. Now let's see the Stateful version of it. The application consists of three main components:

    • Calculator- The Business Interface of the Stateful Bean
    • StatefulCalculator - The Bean implementation of the Business Interface.
    • Client - the JSP file that calls the Bean's instance method.

    Here is the first component. It is the Business Interface:

    package examples.session.stateful;
    import java.util.ArrayList;
    public interface Calculator {

      
    public double calculate (int start, int end, 
                 
    double growthrate, double saving);

       public ArrayList <Integer> getStarts ();
      
    public ArrayList <Integer> getEnds ();
      
    public ArrayList <Double> getGrowthrates ();
      
    public ArrayList <Double> getSavings ();
      
    public ArrayList <Double> getResults ();

    }

    It uses the ArrayList object as a part of the Conversational State. Next is the Bean implementation:

    package examples.session.stateful;
    import javax.ejb.*;
    import java.util.*;
    import java.io.Serializable;

    @Stateful
    public class StatefulCalculator implements Calculator, Serializable {

       public ArrayList <Integer> starts = new ArrayList <Integer> ();
      
    public ArrayList <Integer> ends = new ArrayList <Integer> ();
      
    public ArrayList <Double> growthrates = new ArrayList <Double> ();
      
    public ArrayList <Double> savings = new ArrayList <Double> ();
      
    public ArrayList <Double> results = new ArrayList <Double> ();

       public double calculate (int start, int end, double growthrate, double saving) {
        
    double tmp = Math.pow(1. + growthrate / 12., 12. * (end - start) + 1);
        
    double result = saving * 12. * (tmp - 1) / growthrate;

         starts.add(Integer.valueOf(start));
        
    ends.add(Integer.valueOf(end));
        
    growthrates.add(Double.valueOf(growthrate));
        
    savings.add(Double.valueOf(saving));
        
    results.add(Double.valueOf(result));

         return result;
       
    }

       public ArrayList <Integer> getStarts () {
        
    return starts;
      
    }

       public ArrayList <Integer> getEnds () {
        
    return ends;
      
    }

       public ArrayList <Double> getGrowthrates () {
        
    return growthrates;
      
    }

       public ArrayList <Double> getSavings () {
        
    return savings;
      
    }

       public ArrayList <Double> getResults () {
        
    return results;
      
    }

    }

    This implementation contains an instance variable that is part of Conversational State. Next is the Client in the form of JSP:

    <%@ page import=" examples.session.stateful.*,
                     
    javax.naming.*,
                     
    java.text.*,
                     
    java.util.ArrayList"%>

    <%!
      
    private NumberFormat nf = null;

       public void jspInit () {
        
    nf = NumberFormat.getInstance();
        
    nf.setMaximumFractionDigits(2);
      
    }
    %>

    <%
      
    Calculator cal =
         
    (Calculator) session.getAttribute("sfsb_cal");
      
    if (cal == null) {
        
    try {
          
    InitialContext ctx = new InitialContext();
          
    cal = (Calculator) ctx.lookup(
                  
    "EJB3Trail/StatefulCalculator/local");
          
    session.setAttribute ("sfsb_cal", cal);
        
    } catch (Exception e) {
          
    e.printStackTrace ();
        
    }
      
    }

       String result;

       int start = 25;
      
    int end = 65;
      
    double growthrate = 0.08;
      
    double saving = 300.0;

       try {
        
    start = Integer.parseInt(request.getParameter ("start"));
        
    end = Integer.parseInt(request.getParameter ("end"));
        
    growthrate = Double.parseDouble(request.getParameter ("growthrate"));
        
    saving = Double.parseDouble(request.getParameter ("saving"));

         double res = cal.calculate(start, end, growthrate, saving);
        
    result = nf.format(res);

       } catch (Exception e) {
        
    // e.printStackTrace ();
        
    result = "Not valid";
      
    }
    %>

    <html><body>

    <p>Investment calculator with session history<br/>
    <form action="calculator.jsp" method="POST">
      
    Start age = <input type="text" name="start" value="<%=start%>"><br/>
      
    End age = <input type="text" name="end" value="<%=end%>"><br/>
      
    Annual Growth Rate = <input type="text" name="growthrate" value="<%=growthrate%>"><br/>
      
    Montly Saving = <input type="text" name="saving" value="<%=saving%>"><br/>
      
    <input type="submit" value="Calculate">
      
    <INPUT type="button" value="Close Window" onClick="window.close()">
    </form>
    </p>

    <p>The result from the last calculation: The total investment at end age is
    <b><%=result%></b></p>

    <p><i>Past results</i><br/>
    <%
      int entries = cal.getStarts().size();
    %>
    <table>
      <tr>
        <td>Start Age</td>
        <td>Edn Age</td>
        <td>Annual Growth rate</td>
        <td>Monthly savings</td>
        <td><b>Total investment</b></td>
      </tr>

      <%
        for (int i = 0; i < entries; i++) {
     
    %>

      <tr>
       
    <td><%=cal.getStarts().get(i)%></td>
       
    <td><%=cal.getEnds().get(i)%></td>
       
    <td><%=nf.format(cal.getGrowthrates().get(i))%></td>
       
    <td><%=nf.format(cal.getSavings().get(i))%></td>
       
    <td><%=nf.format(cal.getResults().get(i))%></td>
     
    </tr>

      <%
       
    }
     
    %>
    </table></p>

    </body></html>

    The JSP file takes the input and calculates the result. Apart from that, it shows all the previous results calculated. Thus it takes the advantage of statefulness of the Bean. That brings us to the end of this discussion. The next time I take up this topic I will discuss the basics of the Entity Bean and the process of deploying Beans in JBoss. Till then...


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · In this article I have discussed the basics of the Session Bean development in EJB...
       · Good article about Enterprise Java Beans. However, there is no information...
     

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT