calculator.jsp
01 <%@ page import="trail.slsb.*, javax.naming.*, java.text.*"%>
02 
03 <%!
04   private Calculator cal = null;
05   public void jspInit () {
06     try {
07       InitialContext ctx = new InitialContext();
08       cal = (Calculatorctx.lookup(
09                   "EJB3Trail/StatelessCalculator/local");
10     catch (Exception e) {
11       e.printStackTrace ();
12     }
13   }
14 %>
15 
16 <%
17   String result;
18   int start = 25;
19   int end = 65;
20   double growthrate = 0.08;
21   double saving = 300.0;
22   try {
23     start = Integer.parseInt(request.getParameter ("start"));
24     end = Integer.parseInt(request.getParameter ("end"));
25     growthrate = Double.parseDouble(request.getParameter ("growthrate"));
26     saving = Double.parseDouble(request.getParameter ("saving"));
27 
28     NumberFormat nf = NumberFormat.getInstance();
29     nf.setMaximumFractionDigits(2);
30     result = nf.format(cal.calculate(start, end, growthrate, saving));
31   catch (Exception e) {
32     // e.printStackTrace ();
33     result = "Not valid";
34   }
35 %>
36 
37 <html>
38 <body>
39 
40 <p>Investment calculator<br/>
41 <form action="calculator.jsp" method="POST">
42   Start age = <input type="text" name="start" value="<%=start%>"><br/>
43   End age   = <input type="text" name="end" value="<%=end%>"><br/>
44   Annual Growth Rate = <input type="text" name="growthrate" value="<%=growthrate%>"><br/>
45   Montly Saving = <input type="text" name="saving" value="<%=saving%>"><br/>
46   <input type="submit" value="Calculate">
47   <INPUT type="button" value="Close Window" onClick="window.close()">
48 </form>
49 </p>
50 
51 <p>The result from the last calculation: The balance at the End age is
52 <b><%=result%></b></p>
53 
54 </body>
55 </html>