addfund.jsp
01 <%@ page import="javax.naming.*,
02                  java.text.*,
03                  java.util.*,
04                  trail.entity.beans.*,
05                  trail.entity.basic.*"%>
06 
07 <%!
08   private Calculator cal = null;
09   private NumberFormat nf = null;
10 
11   public void jspInit () {
12     try {
13       InitialContext ctx = new InitialContext();
14       cal = (Calculatorctx.lookup(
15                   "EJB3Trail/EntityCalculator/local");
16     catch (Exception e) {
17       e.printStackTrace ();
18     }
19 
20     nf = NumberFormat.getInstance();
21     nf.setMaximumFractionDigits(2);
22   }
23 %>
24 
25 <%
26     if ("AddFund".equals(request.getParameter("action"))) {
27         cal.addFund (request.getParameter("fundname"),
28             Double.parseDouble(request.getParameter("fundrate")));
29     }
30 %>
31 
32 <html><body>
33 
34 <p>Add a new Fund:<br/>
35 <form action="addfund.jsp" method="POST">
36   Fund Name : <input type="text" name="fundname" value="">
37   Growth rate : <input type="text" name="fundrate" value="0.05">
38   <input type="hidden" name="action" value="AddFund"><br/>
39   <input type="submit" value="Add fund">
40   <INPUT type="button" value="Close Window" onClick="window.close()">
41 </form><br/>
42 
43 <%
44   // Collection <Fund> funds = cal.getFunds();
45   Collection funds = cal.getFunds();
46 %>
47 
48 There are <b><%=funds.size()%></b> funds in the database.<br/>
49 
50 <table>
51 <tr>
52 <td>Fund Name</td>
53 <td>Annual Growth Rate</td>
54 </tr>
55 
56 <%
57     for (Iterator iter = funds.iterator(); iter.hasNext();) {
58     Fund fund = (Funditer.next();
59 %>
60 
61 <tr>
62 <td><%=fund.getName()%></td>
63 <td><%=nf.format(fund.getGrowthrate())%></td>
64 </tr>
65 
66 <%
67   }
68 %>
69 </table></p>
70 
71 </body></html>