JavaBeans are a nice way to encapsulate business logic that might otherwise
appear on a JSP.
Our ultimate goal is to keep the bean interface thin
and push the business logic all the way back to the application server
tier in the form of a JPO. The idea is to have the lightweight bean (living
on the Web tier) call on the JPO to do all the work and then hold the
resulting information (state) to be served up as needed to JSPs. JavaBeans
are simply Java classes that support introspection. They are easy to
use within a JSP. The jsp:useBean associates a JavaBean
with the JSP and ensures that the object is available for the scope specified
in the tag. The bound object can be referenced using the associated id
from one or more JSPs (depending on the scope). The tag syntax is as
follows:
<jsp:useBean id="name" scope="page|request|session|application" beandetails />
Where beandetails is one of:
class="className"
class="className" type="typeName"
beanName="beanName" type="typeName"
type="typeName"
Returning to our "Hello World" example in the
early part of this document, if we assume we have written a HelloWorld
bean to act as the interface to our JPO, we might find the following
usage on a JSP:
<jsp:useBean id="helloBean" scope="session" class="HelloWorld" />
<html>
<body>
<%
helloBean.hello();
%>
</body>
</html>
This shows a HelloWorld bean being defined and given an
id of "helloBean". The hello() method simply calls on
our JPO to generate the "Hello World!" text.
public class HelloWorld implements Serializable
{
public HelloWorld ()
{
}
public void hello()
{
String[] init = new String[] {};
String[] args = new String[] {};
// establish a context ? <details not shown>
int status = JPO.invoke(context, "Hello World", init, "mxMain", args);
}
}