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. |