`calculator.jsp`
`01 <%@ page contentType="text/html; charset=Shift_JIS" %> 02 <%@ page import="trail.sfsb.*, 03 javax.naming.*, 04 java.text.*, 05 java.util.ArrayList"%> 06 07 <%! 08 private NumberFormat nf = null; 09 10 public void jspInit () { 11 nf = NumberFormat.getInstance(); 12 nf.setMaximumFractionDigits(2); 13 } 14 %> 15 16 <% 17 Calculator cal = 18 (Calculator) session.getAttribute("sfsb_cal"); 19 if (cal == null) { 20 try { 21 InitialContext ctx = new InitialContext(); 22 cal = (Calculator) ctx.lookup( 23 "EJB3Trail/StatefulCalculator/local"); 24 session.setAttribute ("sfsb_cal", cal); 25 } catch (Exception e) { 26 e.printStackTrace (); 27 } 28 } 29 30 String result; 31 32 int start = 25; 33 int end = 65; 34 double growthrate = 0.08; 35 double saving = 300.0; 36 37 try { 38 start = Integer.parseInt(request.getParameter ("start")); 39 end = Integer.parseInt(request.getParameter ("end")); 40 growthrate = Double.parseDouble(request.getParameter ("growthrate")); 41 saving = Double.parseDouble(request.getParameter ("saving")); 42 43 double res = cal.calculate(start, end, growthrate, saving); 44 result = nf.format(res); 45 46 } catch (Exception e) { 47 // e.printStackTrace (); 48 result = "Not valid"; 49 } 50 %> 51 52 <html><body> 53 54 <p>セッション履歴機能付きの投資計算プログラム<br/> 55 <form action="calculator.jsp" method="POST"> 56 開始年齢 = <input type="text" name="start" value="<%=start%>"><br/> 57 終了年齢 = <input type="text" name="end" value="<%=end%>"><br/> 58 年成長率 = <input type="text" name="growthrate" value="<%=growthrate%>"><br/> 59 月掛金額 = <input type="text" name="saving" value="<%=saving%>"><br/> 60 <input type="submit" value="計算"> 61 <INPUT type="button" value="閉じる" onClick="window.close()"> 62 </form> 63 </p> 64 65 <p>最新の計算結果：終了年齢での合計投資額は、 66 <b><%=result%></b></p> 67 68 <p><i>過去の計算結果</i><br/> 69 <% 70 int entries = cal.getStarts().size(); 71 %> 72 <table> 73 <tr> 74 <td>開始年齢</td> 75 <td>終了年齢</td> 76 <td>年成長率</td> 77 <td>月掛金額</td> 78 <td><b>合計投資額</b></td> 79 </tr> 80 81 <% 82 for (int i = 0; i < entries; i++) { 83 %> 84 85 <tr> 86 <td><%=cal.getStarts().get(i)%></td> 87 <td><%=cal.getEnds().get(i)%></td> 88 <td><%=nf.format(cal.getGrowthrates().get(i))%></td> 89 <td><%=nf.format(cal.getSavings().get(i))%></td> 90 <td><%=nf.format(cal.getResults().get(i))%></td> 91 </tr> 92 93 <% 94 } 95 %> 96 </table></p> 97 98 </body></html>`