`calculator.jsp`
`001 <%@ page contentType="text/html; charset=Shift_JIS" %> 002 <%@ page import="trail.interceptor.*, 003 javax.naming.*, 004 java.text.*, 005 java.util.ArrayList"%> 006 007 <%! 008 private NumberFormat nf = null; 009 010 public void jspInit () { 011 nf = NumberFormat.getInstance(); 012 nf.setMaximumFractionDigits(2); 013 } 014 %> 015 016 <% 017 Calculator cal = 018 (Calculator) session.getAttribute("interceptor_cal"); 019 if (cal == null) { 020 try { 021 InitialContext ctx = new InitialContext(); 022 cal = (Calculator) ctx.lookup( 023 "EJB3Trail/InterceptorCalculator/local"); 024 session.setAttribute ("interceptor_cal", cal); 025 } catch (Exception e) { 026 e.printStackTrace (); 027 } 028 } 029 030 String result; 031 032 int start = 25; 033 int end = 65; 034 double growthrate = 0.08; 035 double saving = 300.0; 036 037 // clear the call trace 038 cal.setTrace (""); 039 040 try { 041 start = Integer.parseInt(request.getParameter ("start")); 042 end = Integer.parseInt(request.getParameter ("end")); 043 growthrate = Double.parseDouble(request.getParameter ("growthrate")); 044 saving = Double.parseDouble(request.getParameter ("saving")); 045 046 double res = cal.calculate(start, end, growthrate, saving); 047 result = nf.format(res); 048 049 } catch (Exception e) { 050 // e.printStackTrace (); 051 result = "Not valid"; 052 } 053 %> 054 055 <html><body> 056 057 <p>セッション履歴付き投資計算プログラム<br/> 058 <form action="calculator.jsp" method="POST"> 059 開始年齢 = <input type="text" name="start" value="<%=start%>"><br/> 060 終了年齢 = <input type="text" name="end" value="<%=end%>"><br/> 061 年成長率 = <input type="text" name="growthrate" value="<%=growthrate%>"><br/> 062 月掛金額 = <input type="text" name="saving" value="<%=saving%>"><br/> 063 <input type="submit" value="計算"> 064 <INPUT type="button" value="閉じる" onClick="window.close()"> 065 </form> 066 </p> 067 068 <p>最新の計算結果：終了年齢での合計投資額は、 069 <b><%=result%></b></p> 070 071 <p><i>過去の計算結果</i><br/> 072 <% 073 int entries = cal.getStarts().size(); 074 %> 075 <table> 076 <tr> 077 <td>開始年齢</td> 078 <td>終了年齢</td> 079 <td>年成長率</td> 080 <td>月掛金額</td> 081 <td><b>合計投資額</b></td> 082 </tr> 083 084 <% 085 ArrayList starts = cal.getStarts(); 086 ArrayList ends = cal.getEnds (); 087 ArrayList growthrates = cal.getGrowthrates (); 088 ArrayList savings = cal.getSavings (); 089 ArrayList results = cal.getResults (); 090 for (int i = 0; i < entries; i++) { 091 %> 092 093 <tr> 094 <td><%=starts.get(i)%></td> 095 <td><%=ends.get(i)%></td> 096 <td><%=nf.format(growthrates.get(i))%></td> 097 <td><%=nf.format(savings.get(i))%></td> 098 <td><%=nf.format(results.get(i))%></td> 099 </tr> 100 101 <% 102 } 103 %> 104 </table></p> 105 106 <p><i>直前の呼び出しトレース</i><br/> 107 <%=cal.getTrace()%> 108 </p> 109 110 </body></html>`