addfund.jsp
01 <%@ page import="javax.naming.*,
02                  java.text.*,
03                  java.util.*, org.jboss.security.*,
04                  trail.entity.beans.*, trail.security.*,
05                  java.security.Principal"%>
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/SecureCalculator/local");
16     catch (Exception e) {
17       e.printStackTrace ();
18     }
19 
20     nf = NumberFormat.getInstance();
21     nf.setMaximumFractionDigits(2);
22   }
23 %>
24 
25 <html>
26 
27 <%
28     if ("AddFund".equals(request.getParameter("action"))) {
29       try {
30         cal.addFund (request.getParameter("fundname"),
31             Double.parseDouble(request.getParameter("fundrate")));
32       catch (Exception e) {
33 %>
34 <head><meta http-equiv="REFRESH" content="0; URL=error.html"></head>
35 <%
36         return;
37       }
38     else if ("Logout".equals(request.getParameter("action"))) {
39       ((HttpSessionrequest.getSession()).invalidate ();
40       SecurityAssociation.clear ();
41 %>
42 <head><meta http-equiv="REFRESH" content="0; URL=addfund.jsp"></head>
43 <%
44       return;
45     }
46 %>
47 
48 <body>
49 
50 <p><form action="addfund.jsp" method="POST">
51 The current user is <b><%=((PrincipalSecurityAssociation.getPrincipal()).getName()%></b>
52   <input type="hidden" name="action" value="Logout"><br/>
53   <input type="submit" value="Change user">
54 </form></p>
55 
56 <p>Add a new Fund:<br/>
57 <form action="addfund.jsp" method="POST">
58   Fund Name : <input type="text" name="fundname" value="">
59   Growth rate : <input type="text" name="fundrate" value="0.05">
60   <input type="hidden" name="action" value="AddFund"><br/>
61   <input type="submit" value="Add fund">
62   <INPUT type="button" value="Close Window" onClick="window.close()">
63 </form><br/>
64 
65 <%
66   // Collection <Fund> funds = cal.getFunds();
67   Collection funds = cal.getFunds();
68 %>
69 
70 There are <b><%=funds.size()%></b> funds in the database.<br/>
71 
72 <table>
73 <tr>
74 <td>Fund Name</td>
75 <td>Annual Growth Rate</td>
76 </tr>
77 
78 <%
79     for (Iterator iter = funds.iterator(); iter.hasNext();) {
80     Fund fund = (Funditer.next();
81 %>
82 
83 <tr>
84 <td><%=fund.getName()%></td>
85 <td><%=nf.format(fund.getGrowthrate())%></td>
86 </tr>
87 
88 <%
89   }
90 %>
91 </table></p>
92 
93 </body></html>