calculator.jsp
01 <%@ page import="trail.sfsb.*,
02                  javax.naming.*,
03                  java.text.*,
04                  java.util.ArrayList"%>
05 
06 <%!
07   private NumberFormat nf = null;
08 
09   public void jspInit () {
10     nf = NumberFormat.getInstance();
11     nf.setMaximumFractionDigits(2);
12   }
13 %>
14 
15 <%
16   Calculator cal =
17       (Calculatorsession.getAttribute("sfsb_cal");
18   if (cal == null) {
19     try {
20       InitialContext ctx = new InitialContext();
21       cal = (Calculatorctx.lookup(
22                   "EJB3Trail/StatefulCalculator/local");
23       session.setAttribute ("sfsb_cal", cal);
24     catch (Exception e) {
25       e.printStackTrace ();
26     }
27   }
28 
29   String result;
30 
31   int start = 25;
32   int end = 65;
33   double growthrate = 0.08;
34   double saving = 300.0;
35 
36   try {
37     start = Integer.parseInt(request.getParameter ("start"));
38     end = Integer.parseInt(request.getParameter ("end"));
39     growthrate = Double.parseDouble(request.getParameter ("growthrate"));
40     saving = Double.parseDouble(request.getParameter ("saving"));
41 
42     double res = cal.calculate(start, end, growthrate, saving);
43     result = nf.format(res);
44 
45   catch (Exception e) {
46     // e.printStackTrace ();
47     result = "Not valid";
48   }
49 %>
50 
51 <html><body>
52 
53 <p>Investment calculator with session history<br/>
54 <form action="calculator.jsp" method="POST">
55   Start age = <input type="text" name="start" value="<%=start%>"><br/>
56   End age   = <input type="text" name="end" value="<%=end%>"><br/>
57   Annual Growth Rate = <input type="text" name="growthrate" value="<%=growthrate%>"><br/>
58   Montly Saving = <input type="text" name="saving" value="<%=saving%>"><br/>
59   <input type="submit" value="Calculate">
60   <INPUT type="button" value="Close Window" onClick="window.close()">
61 </form>
62 </p>
63 
64 <p>The result from the last calculation: The total investment at end age is
65 <b><%=result%></b></p>
66 
67 <p><i>Past results</i><br/>
68 <%
69   int entries = cal.getStarts().size();
70 %>
71 <table>
72 <tr>
73 <td>Start Age</td>
74 <td>Edn Age</td>
75 <td>Annual Growth rate</td>
76 <td>Monthly savings</td>
77 <td><b>Total investment</b></td>
78 </tr>
79 
80 <%
81   for (int i = 0; i < entries; i++) {
82 %>
83 
84 <tr>
85 <td><%=cal.getStarts().get(i)%></td>
86 <td><%=cal.getEnds().get(i)%></td>
87 <td><%=nf.format(cal.getGrowthrates().get(i))%></td>
88 <td><%=nf.format(cal.getSavings().get(i))%></td>
89 <td><%=nf.format(cal.getResults().get(i))%></td>
90 </tr>
91 
92 <%
93   }
94 %>
95 </table></p>
96 
97 </body></html>