About JavaBeans

JavaBeans encapsulate business logic that might otherwise appear on a JSP. The ultimate goal is to keep the Bean interface thin and push the business logic into the application server tier in the form of a JPO. The idea is to have the lightweight Bean (living on the Web tier) call the JPO to do all the work and then hold the resulting information (state) to serve up as needed to JSPs. JavaBeans are simply Java classes that support introspection and 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"

Using the "Hello World" example, if you assume there is a HelloWorld Bean to act as the interface to the JPO, then you might find the following usage in 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 the 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);
  }
}