calculator.jsp
001 <%@ page import="javax.naming.*,
002                  java.text.*,
003                  java.util.*, org.jboss.security.*,
004                  trail.entity.beans.*, trail.security.*,
005                  java.security.Principal"%>
006 
007 <%!
008   private Calculator cal = null;
009   private NumberFormat nf = null;
010 
011   public void jspInit () {
012     try {
013       InitialContext ctx = new InitialContext();
014       cal = (Calculatorctx.lookup(
015                   "EJB3Trail/SecureCalculator/local");
016     catch (Exception e) {
017       e.printStackTrace ();
018     }
019 
020     nf = NumberFormat.getInstance();
021     nf.setMaximumFractionDigits(2);
022   }
023 %>
024 
025 <html>
026 
027 <%
028     String result = "Not Valid";
029     if ("Calculate".equals(request.getParameter("action"))) {
030         double res = -1;
031         res = cal.calculate (Integer.parseInt(request.getParameter("fund")),
032                              Integer.parseInt(request.getParameter("investor")),
033                              Double.parseDouble(request.getParameter("saving")));
034         if (res != -1) {
035             result = nf.format(res);
036         }
037     else if ("Logout".equals(request.getParameter("action"))) {
038       ((HttpSessionrequest.getSession()).invalidate ();
039       SecurityAssociation.clear ();
040 %>
041 <head><meta http-equiv="REFRESH" content="0; URL=calculator.jsp"></head>
042 <%
043       return;
044     }
045 %>
046 
047 <body>
048 
049 <p><form action="calculator.jsp" method="POST">
050 The current user is <b><%=((PrincipalSecurityAssociation.getPrincipal()).getName()%></b>
051   <input type="hidden" name="action" value="Logout"><br/>
052   <input type="submit" value="Change user">
053 </form></p>
054 
055 <p>The Investment calculator<br/>
056 <form action="calculator.jsp" method="POST">
057   Choose a fund :
058   <select name="fund">
059 <%
060   Collection funds = cal.getFunds ();
061   for (Iterator iter = funds.iterator(); iter.hasNext();) {
062     Fund fund = (Funditer.next();
063 %>
064     <option value="<%=fund.getId()%>"><%=fund.getName()%></option>
065 <%
066   }
067 %>
068   </select>
069   and an investor:
070   <select name="investor">
071 <%
072   Collection investors = cal.getInvestors ();
073   for (Iterator iter = investors.iterator(); iter.hasNext();) {
074     Investor investor = (Investoriter.next();
075 %>
076     <option value="<%=investor.getId()%>"><%=investor.getName()%></option>
077 <%
078   }
079 %>
080   </select><br/>
081   Monthly saving = <input type="text" name="saving" value="100">
082   <input type="hidden" name="action" value="Calculate">
083   <input type="submit" value="Calculate">
084   <INPUT type="button" value="Close Window" onClick="window.close()">
085 </form><br/>
086 The total investment is <%=result%><br/>
087 <br/>
088 All records from past calculations in the database<br/>
089 <table>
090 <tr>
091 <td>Time stamp</td>
092 <td>Fund</td>
093 <td>Investor</td>
094 <td>Monthly savings</td>
095 <td><b>Total investment</b></td>
096 </tr>
097 
098 <%
099     Collection records = cal.getRecords ();
100     for (Iterator iter = records.iterator(); iter.hasNext();) {
101         TimedRecord record = (TimedRecorditer.next();
102 %>
103 
104 <tr>
105 <td><%=record.getTs()%></td>
106 <td><%=record.getFund().getName()%></td>
107 <td><%=record.getInvestor().getName()%></td>
108 <td><%=nf.format(record.getSaving())%></td>
109 <td><%=nf.format(record.getResult())%></td>
110 </tr>
111 
112 <%
113     }
114 %>
115 </table>
116 
117 </p>
118 
119 </body></html>