JSP Components
 
Coding the JavaBeans Component
 
The following description of the AccountBean code is quite brief.
 
The AccountBean JavaBeans component is created when the end-user first accesses the Account.jsp page. (This action is specified by the jsp:useBean tag in the Account.jsp file.) The AccountBean class accesses the entity bean implemented by the AccountEJB class. The constructor of the AccountBean class locates the entity bean's home interface by invoking the lookup method:
 
public AccountBean()
{

try
{
Context ic = new InitialContext();
java.lang.Object objref = ic.lookup("java:comp/env/ejb/Account");
accountHome =
(AccountHome) PortableRemoteObject.narrow(objref, AccountHome.class);
}
catch (Exception re)
{
System.err.println ("Couldn't locate Account Home");
re.printStackTrace();
}
reset();
}
 
The end-user indicates the action parameter by selecting a radio button on the Account.jsp page. The web server assigns the parameter value to the action property of the AccountBean. Invoked by a scripting element in the Account.jsp page, the processRequest method of the AccountBean checks the value of the action property. If the action property is create, for example, the processRequest method creates a new entity bean. Here is the code for the processRequest method:
 
public String processRequest()
{

String message = "";

System.out.println("Process request called ");
System.out.println(this);

try
{

if( action.equals("create") )
{

account =accountHome.create(id, firstName, lastName, balance);
message = "Created account `" + id + "`";

}
else if( action.equals("debit") )
{

account = accountHome.findByPrimaryKey(id);
account.debit(amount);
loadFromEJB();
message = "Debited account `" + id + "` by $" + amount;

}
else if( action.equals("credit") )
{
account = accountHome.findByPrimaryKey(id);
account.credit(amount);
loadFromEJB();
message = "Credited account `" + id + "` by $" + amount;

}
else if( action.equals("find") )
{

account = accountHome.findByPrimaryKey(id);
loadFromEJB();
message = "Found account `" + id;

}

} // try

catch (Exception e) {
message = e.toString();
}

return message;
}