JavaServer Pages and JavaBean Components, Part III

by Richard G. Baldwin

Java Programming, Lecture Notes # 766

July 10, 2000


What is JSP?


Previous Lessons

Previous lessons have explained the use of the following JSP elements:

A broad topic

Because of the breadth of the JavaBean component aspect of JSP, I have broken this topic into several lessons.  The previous lesson taught you how to use the <jsp:setProperties...> tag and the <jsp:getProperties...> tag to set and get the property values for a Bean that adheres to design patterns.

No requirement for design patterns

I also told you in that lesson that it is not necessary to use design patterns to expose the properties, methods, and events when you design a Bean.  However, if you don't use design patterns, you will be required to do some extra work.

As promised, in this lesson, I am going to show you how to design a Bean that doesn't use design patterns, at least insofar as properties are concerned.  I will also show you how to use that Bean with JSP.

The Java interface

Five or six times during each semester, I tell my students, "If you don't understand the Java interface, you don't understand Java."  This is an important point, because even experienced C++ object-oriented programmers may not understand the concept of an interface as used in Java.

In case you don't understand the Java interface, please refer to the appropriate lessons in my online Java tutorials.

Two approaches

There are two ways that the Bean developer can notify the user's development environment about the properties, methods, and events of a Bean:

The BeanInfo interface

I explained the use of design patterns in the previous lesson.

This lesson concentrates on the use of the BeanInfo interface.  In this explanation, I will assume that you have a good understanding of Beans in general.  In particular, I will gloss over some of the subtle nuances that you might need to consider when designing a Bean that doesn't make use of design patterns.

Explicit specification of Bean properties

The Bean developer can notify the user's development environment about Bean properties by doing the following:

A meta object

In programmer jargon, a meta object is an object that provides information about something else.  I sometimes refer to it as an agent object because it can provide information about the thing that it represents.

For example, an object of the File class can provide information about the physical file that it represents, such as the length of the file, and the absolute path to the file.

The BeanInfo object

In this case, the development environment (such as the JSP engine) will use introspection to cause an object to be instantiated from the class that implements the BeanInfo interface and represents the Bean.  The JSP engine can then ask that object questions about the Bean that it represents.

For example, the BeanInfo object can answer such questions as "What are the names of the methods used to set and get property values for the Bean that you represent?"

The Sample Bean

Doesn't adhere to design patterns

Figure 1 shows a Bean class that does not adhere to design patterns.

This Bean is very similar to the sample Beans used in previous lessons.  However, in this case I prepended the names of the setter and getter methods with the letter "x".  As a result, the setter and getter methods do not adhere to JavaBean design patterns.

I highlighted those method names in red to make them easy for you to spot in Figure 1.

JSP cannot use reflection alone

Because the Bean does not adhere to design patterns, the JSP engine cannot use reflection alone to determine the names of the setter and getter methods, as was the case in a previous lesson.

In this case, it is necessary to explicitly provide that information to the JSP engine.

Explicit specification of the names of the setter and getter methods

Explicit specification is accomplished in Figure 2, which defines a class with the proper name that indirectly implements the BeanInfo interface.

BeanInfo interface details

Figure 3 provides an excerpt from the Sun documentation that explains the use of the BeanInfo interface in more detail.

SimpleBeanInfo class

The SimpleBeanInfo class is similar to the adapter classes of the Abstract Windows Toolkit.  Like the adapter classes in the AWT, the SimpleBeanInfo class implements the BeanInfo interface and provides a default definition for all of the methods declared in that interface.

Indirect implementation of BeanInfo

To provide information about properties only (while ignoring methods and events), I can extend the SimpleBeanInfo class and override the method named getPropertyDescriptors().

If I am only interested in providing explicit information about properties, it isn't necessary for me to concern myself with the methods of the BeanInfo interface having to do with methods and events.

The getPropertyDescriptors() method

Figure 4 provides an excerpt from the Sun documentation describing the method named getPropertyDescriptors().

This method returns an array of references to PropertyDescriptor objects, where each property of the target Bean is represented by one element in the array.

The purpose of this method is to provide information about properties to the Bean user's development environment.

The PropertyDescriptor class

Figure 5 provides an excerpt from the Sun documentation describing one of the overloaded constructors for the PropertyDescriptor class.  (This class has several overloaded constructors, but this is the one used in the sample program in this lesson.)

Understanding the Class class

The only thing that might be difficult to understand about this constructor is the second parameter that requires a reference to an object of the class named Class.

This object identifies the class file for the Bean.  The use of the Class class is a little bit complicated.  If you don't know about the class named Class, see my online Java programming tutorials.

The jspBeanTest002BeanInfo class

Now please refer back to Figure 2, which shows my simple BeanInfo class for this sample program.  I refer to this as a BeanInfo class because it implements the BeanInfo interface.

The required name

This BeanInfo class, named jspBeanTest002BeanInfo, satisfies the required naming convention to associate it with the primary Bean class named jspBeanTest002.

Implements the BeanInfo interface

The BeanInfo class implements the BeanInfo interface indirectly by extending the class named SimpleBeanInfo.

Overrides getPropertyDescriptors()

The BeanInfo class provides explicit information about the Bean's properties by overriding the method named getPropertyDescriptors().

I highlighted the overridden method signature in red in Figure 2 to make it easy to spot.

PropertyDescriptor object represents the property

The BeanInfo class instantiates an object of the PropertyDescriptor class that provides the names of the setter and getter methods for the simple property (named size) of the target class named jspBeanTest002.  I highlighted this in green to make it easy to spot in Figure 2.

The names of the setter and getter methods are xgetSize and xsetSize. (Because of the x, they don't satisfy the JavaBean design-pattern requirements for properties.)

Populates and returns array

The BeanInfo class instantiates and populates a single-element array of the PropertyDescriptor class with the new PropertyDescriptor object, and returns a reference to the array.

If the target Bean had more than one property, the array would have one element for each property.

The magic of JavaBean components

The JSP engine can then use introspection and the getPropertyDescriptors() method to learn about the names of the setter and getter methods.

The Sample JSP Page

Figure 6 shows a JSP page that is capable of processing the Bean named jspBeanTest002.

More magic of JavaBean components

It is important to recognize that the JSP programmer who uses the Bean doesn't need to know whether the Bean

This is part of the magic of JavaBean components.

Not true for explicit setter invocation

Of course, this would not be the case if the JSP programmer were to forego use of the jsp:setProperty tag and invoke the setSize() method directly.  In that case, the JSP programmer would have to know the name of the setter method.

JSP behavior

As in a previous lesson, this JSP page

The output

Figure 7 shows a replica of my browser output when it is used to access the page named jsp010.jsp.  Hopefully, by now, the output is what you would expect to see based on your growing knowledge of JSP.

JSP Syntax Summary


Output comments:

Hidden comments:
<%-- hidden comment --%>

<%! declarations %>

<%= expression %>

<% code fragment %>

Include Directive:
<%@ include file="relativeURL" %>

Page Directive:
<%@ page
  [ language="java" ]
  [ extends="package.class" ]
  [ import= "{ package.class |
    package.* }, ..." ]
  [ session="true|false" ]
  [ buffer="none|8kb|sizekb" ]
  [ autoFlush="true|false" ]
  [ isThreadSafe="true|false" ]
  [ info="text" ]
  [ errorPage="relativeURL" ]
  [ contentType="mimeType [
    ;charset=characterSet ]" |
  [ isErrorPage="true|false" ]

jsp:forward tag:
<jsp:forward page=

jsp:useBean tag:
    {       class="package.class" |
            type="package.class" |
            class="package.class" type="package.class" |
    {       /> | > other tags </jsp:useBean>  }

   {  property= "*" |
      property="propertyName" value=
          "{ string | <%= expression %> }"

<jsp:getProperty name="beanInstanceName"
                            property="propertyName" />


About the author


