<!– copyright (c) 1999-2000 by bea systems, inc. all rights reserved.–>
<!– here we use a java string ("pagetitle") to set
the same phrase as the title and as a head.–>
<html>
<head>
<title>
<%= pagetitle %>
</title>
</head>
<body bgcolor=#ffffff>
<font face="helvetica">
<h2>
<font color=#db1260>
<%= pagetitle %>
</font>
</h2>
<!– using the "import" feature –>
<%@ page import="
javax.naming.*,
javax.ejb.*,
java.rmi.remoteexception,
java.rmi.remote,
java.util.*,
examples.ejb.basic.beanmanaged.*
"%>
<!– here, we declare a class method –>
<%!
string pagetitle = "jsp example using ejbean-managed persistence";
string url = "t3://localhost:7001";
string accountid = "10020";
// declaring a java class
public context getinitialcontext() throws exception {
properties p = new properties();
p.put(context.initial_context_factory,
"weblogic.jndi.wlinitialcontextfactory");
p.put(context.provider_url, url);
return new initialcontext(p);
}
string getstacktraceasstring(exception e)
{
// dump the stack trace to a buffered stream, then send its contents
// to the jspwriter.
bytearrayoutputstream ostr = new bytearrayoutputstream();
e.printstacktrace(new printwriter(ostr));
return(ostr.tostring());
}
%>
<!– note that the following java code will be inserted into the body
of the servlet –>
<%
double amount = 100;
double balance = 3000;
accountpk accountkey = new accountpk();
accountkey.accountid = accountid;
try {
// contact the accountbean container (the "accounthome") through jndi.
context ctx = getinitialcontext();
accounthome home = (accounthome) ctx.lookup("beanmanaged.accounthome");
%>
<p>
<b>looking up account <%= accountid %> …</b>
<%
account ac = null;
try {
ac = (account) home.findbyprimarykey(accountkey);
}
catch (exception ee) {
out.println("<br>did not find "+ accountid);
}
if (ac == null) {
out.print("<br>account " + accountid +
" being created; opening balance is $" + balance);
ac = home.create(accountid, balance);
}
else {
out.print("<br>account " + accountid +
" found; balance is $" + ac.balance());
}
%>
<h2>part a:</h2>
deposit and attempt to withdraw more than the current
account balance. an application-specific exception should be thrown:-
<p>
current balance: $ <%= ac.balance() %> <br>
depositing: $ <%= amount %>
<p>
<%
// deposit the amount into the account
balance = ac.deposit(amount);
%>
new balance: $ <%= balance %>
<p>
<h2>partb:</h2>
withdraw an amount greater than
current balance. expecting an exception…
<p>
<%
amount = balance + 10;
try {
balance = ac.withdraw(amount);
out.print("error: an exception should have been thrown.");
}
catch (processingerrorexception pe) {
out.print("received expected processing error:" + pe);
out.print("<pre>" + getstacktraceasstring(pe) + "</pre>");
}
%>
<h2>part c</h2>
create some new accounts, with different initial balances.
find all the accounts with a balance greater than a specific value.
when finished, the new accounts are removed.
<p>
<%
int numaccounts = 5;
long now = system.currenttimemillis();
vector v = new vector();
for (int i = 0; i < numaccounts; i++) {
string id = "" + now + i; // create unique account id
balance = i*100; // initial balance
v.addelement(home.create(id, balance));
%>
<br>created account: <%= id %> with balance: $ <%= balance %>
<%
} // end of creating accounts for loop
if (v.size() == numaccounts) {
out.print("<p>success: " + numaccounts + " accounts successfully created");
}
else {
out.print("<p>error: only " + v.size() +
" accounts were created successfully");
}
%>
<%
double balancegreaterthan = 700;
%>
<p>querying for accounts with a balance greater than <%= balancegreaterthan %>
<table><tr><th account id><th balance></tr>
<%
enumeration e = home.findbigaccounts(balancegreaterthan);
if (e != null) {
while (e.hasmoreelements()) {
account bigaccount= (account) e.nextelement();
%>
<tr>
<td><%= bigaccount.getprimarykey() %></td>
<td><%= bigaccount.balance() %></td>
</tr>
<%
// thread.sleep(1000);
}
}
%>
</table>
<p>now removing the accounts we just created…
<%
for (int i = 0; i < numaccounts; i++) {
string id = string.valueof(now) + string.valueof(i);
((account)(v.elementat(i))).remove();
out.print("<br>removed account: " +id);
}
// catch exceptions
}
catch (processingerrorexception pe) {
out.print("<p>unexpected processing error: " + pe +
"<pre>"+getstacktraceasstring(pe)+"</pre>");
}
catch (exception e) {
out.print("<p>:::::::::::::: unexpected error :::::::::::::::::");
out.print("<pre>"+getstacktraceasstring(e)+"</pre>");
}
finally {
%>
<p>completed ejb operations at <%= new date() %>
<p><hr width=80%>
<%
}
%>
<p>
<font size=-1>
copyright © 1999-2000 by bea systems, inc. all rights reserved.
</font>
</font>
</body>
</html>
