addinvestor.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 ("AddInvestor".equals(request.getParameter("action"))) {
29       try {
30         cal.addInvestor (request.getParameter("investorname"),
31             Integer.parseInt(request.getParameter("investorstart")),
32             Integer.parseInt(request.getParameter("investorend")));
33       catch (Exception e) {
34 %>
35 <head><meta http-equiv="REFRESH" content="0; URL=error.html"></head>
36 <%
37         return;
38       }
39     else if ("Logout".equals(request.getParameter("action"))) {
40       ((HttpSessionrequest.getSession()).invalidate ();
41       SecurityAssociation.clear ();
42 %>
43 <head><meta http-equiv="REFRESH" content="0; URL=addinvestor.jsp"></head>
44 <%
45       return;
46     }
47 %>
48 
49 <body>
50 
51 <p><form action="addinvestor.jsp" method="POST">
52 The current user is <b><%=((PrincipalSecurityAssociation.getPrincipal()).getName()%></b>
53   <input type="hidden" name="action" value="Logout"><br/>
54   <input type="submit" value="Change user">
55 </form></p>
56 
57 <p>Add a new investor:<br/>
58 <form action="addinvestor.jsp" method="POST">
59   Name : <input type="text" name="investorname" value=""><br/>
60   Start age = <input type="text" name="investorstart" value="25">
61   End age = <input type="text" name="investorend" value="65">
62   <input type="hidden" name="action" value="AddInvestor"><br/>
63   <input type="submit" value="Add Investor">
64   <INPUT type="button" value="Close Window" onClick="window.close()">
65 </form><br/>
66 
67 <%
68   // Collection <Investor> investors = cal.getInvestors();
69   Collection investors = cal.getInvestors();
70 %>
71 
72 There are <b><%=investors.size()%></b> investors in the database.<br/>
73 
74 <table>
75 <tr>
76 <td>Name</td>
77 <td>Start age</td>
78 <td>End age</td>
79 </tr>
80 
81 <%
82     for (Iterator iter = investors.iterator(); iter.hasNext();) {
83     Investor investor = (Investoriter.next();
84 %>
85 
86 <tr>
87 <td><%=investor.getName()%></td>
88 <td><%=investor.getStart()%></td>
89 <td><%=investor.getEnd()%></td>
90 </tr>
91 
92 <%
93   }
94 %>
95 </table></p>
96 
97 </body></html>