<!-- Copyright (c) 1999 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. We need the javax.naming.* for
the Context used to lookup the Trader home interface.
The samples.j2ee.ejb.basic.statelessSession.* is for the
Trader related classes and stubs and skeletons.
See the documentation for the WLE Stateless Session Bean Sample
to understand the usage of the Trader related classes and
stubs and skeletons. -->
<%@ page import="
javax.naming.*,
samples.j2ee.ejb.basic.statelessSession.*
"%>
<!-- Declarations section for variables and methods of class scope.
Class scope objects are shared between multiple threads executing
in the same instance of the servlet that is compiled from the JSP.
Declarations are used to declare scripting language constructs that
are available to all other scripting elements. Declarations are
initialized when the JSP page is initialized and are made available
to other declarations, scriptlets, and expressions. -->
<%!
static final String pagetitle = "WLEC JSP Stateless Session Bean Sample";
static final String customerName = "Erin"; // Default name for the customer
static final int maxTransaction = 5; // no. transactions to do
// The getInitialContext() method returns an InitialContext, which the page will
// use to look up the Trader home interface.
public Context getInitialContext() throws Exception {
Hashtable env = new Hashtable();
// Specify the name of the connection pool.
env.put(Context.PROVIDER_URL, "wlepool://StatelessSessionPool");
// Name of the factory to access WLE domain and global naming service.
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.beasys.jndi.WLEInitialContextFactory");
return new InitialContext(env);
}
String getStackTraceAsString(Exception e)
{
// Dump the stack trace to a buffered stream, then send it's 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 -->
<%
Context ctx = null; // To hold JNDI context
Object objref = null; // to hold object reference
String stockName = null; // Name of a stock
int numberOfShares = 0; // No. of shares to trade
double cashBalance = 0.0; // To hold balance between sessions
TraderHome brokerage = null; // To hold home interface
Trader trader = null; // To hold trader object
TradeResult tradeResult = null; // To hold results from a trade
%>
<p><b>Getting Initial Context</b>
<%
try {
// Create a new initial context based on the url, user, and password
ctx = getInitialContext();
if ( ctx == null ){
out.print("<p>Initial context is null!");
}
%>
<p><b>Looking Up Home and Creating Trader</b>
<%
// do a JNDI lookup for the EJB;defined in the deployment descriptor
objref = ctx.lookup("statelessSession.TraderHome");
/* Create a trader object, who'll later help us execute trades
* The lookup has resulted in an Object. We know
* this object is actually a reference of type TraderHome,
* so the reference is cast to that type:
*/
brokerage = (TraderHome) objref;
// Create the EJB on the WLE server.
trader = brokerage.create();
%>
<p><b>Buy and Sell with TraderBean</b>
<ul>
<%
/* Unlike the statefulSession example,
* we have to keep track of the balance over the
* life of our use of the session bean
*/
for (int i = 1 ; i <= maxTransaction; i++) {
out.print("<p><li>Start of Transaction " + i + " for " +
customerName);
/* Buying
* Stock symbol must be found in the deployment descriptor's environment
* properties section. TraderBean EJB will check the validity of the
* symbol, and its price using JNDI lookup on the environment
* properties.
*/
stockName = "BEAS";
numberOfShares = 100 * i;
out.print("<br>Buying " + numberOfShares + " of " + stockName);
// buy() is executed on the TraderBean EJB in the WLE Server
tradeResult = trader.buy(customerName, stockName, numberOfShares);
out.print("...Bought " + tradeResult.numberTraded + " at $" +
tradeResult.priceSoldAt);
// Keep track of the change in the Cash Account
cashBalance = cashBalance - (tradeResult.numberTraded *
tradeResult.priceSoldAt);
// Selling
stockName = "INTL";
numberOfShares = 100 * (i+1);
out.print("<br>Selling " + numberOfShares + " of " + stockName);
// sell() is executed on the TraderBean EJB in the WLE Server
tradeResult = trader.sell(customerName, stockName, numberOfShares);
out.print("...Sold " + tradeResult.numberTraded + " at $" +
tradeResult.priceSoldAt);
// Keep track of the change in the Cash Account
cashBalance = cashBalance + (tradeResult.numberTraded *
tradeResult.priceSoldAt);
// Print change in Cash Account
out.print("<br>Change in Cash Account: $" + cashBalance);
out.print("<br>End of Transaction " + i + "\n");
}
%>
</ul>
<p><b>Removing Trader</b>
<%
// Remove TraderBean EJB from the WLE server.
trader.remove();
}
catch (ProcessingErrorException pe) {
out.print("<p>Processing Error: " + pe);
out.print("<p>" + getStackTraceAsString(pe));
}
catch (Exception e) {
out.print("<p>:::::::::::::: Error :::::::::::::::::");
out.print("<p> Stack Trace:" + getStackTraceAsString(e));
}
%>
<p><b>Trader Removed and Sample Complete!</b>
<p>
<font size=-1>
Copyright © 1999 by BEA Systems, Inc. All Rights Reserved.
</font>
</font>
</body>
</html>