Home arrow Java arrow Page 4 - Session Beans in EJB 3.0
JAVA

Session Beans in EJB 3.0


Enterprise applications revolve mostly around three aspects of data: transfer of data, retrieval of data and transformation of data by applying the business rules of a particular organization. Since these three aspects are specific to an organization, Enterprise Java Beans (EJB) are known as Business Specific Components. Even among the three aspects, the former two are more generic in nature whereas the latter is closer to the policies of a given organization.

Author Info:
By: A.P.Rajshekhar
Rating: 4 stars4 stars4 stars4 stars4 stars / 7
March 28, 2007
TABLE OF CONTENTS:
  1. · Session Beans in EJB 3.0
  2. · Stateful Session Beans
  3. · Implementing Session Beans
  4. · Session Beans in the Real World

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

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.

blog comments powered by Disqus
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...

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials