JavaEnterprise JavaJavaServer Pages and JavaBean Components, Part III

JavaServer Pages and JavaBean Components, Part III content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.


What is JSP?

JSP makes it possible to embed Java code fragments into special HTML tags.  A JSP engine automatically creates, compiles, and executes servlets to implement the behavior of the combined HTML and embedded Java code.

Previous Lessons

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

  • Output comments
  • Hidden comments
  • Declarations
  • Expressions
  • Scriptlets
  • The include directive
  • The page directive
  • The <jsp.forward> tag
  • The <jsp:useBean…/> tag
  • The <jsp:setProperties…> tag
  • The <jsp:getProperties…> tag

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:
JavaServer Pages and JavaBean Components, Part II
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:

  • Use of design patterns
  • Use of a class that implements the BeanInfo interface

The BeanInfo interface

I explained the use of design patterns in the previous lesson.
JavaServer Pages and JavaBean Components, Part II

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:

  • Define a class that implements the BeanInfo interface and provides concrete definitions for some or all of the methods declared in that interface.
  • Form the name of the new class by appending BeanInfo to the end of the Bean’s class name.
  • Deliver a compiled version of this BeanInfo class along with the other class files that comprise the Bean.

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

  • Uses design patterns for setter or getter methods, or
  • Uses explicit specification of the setter and getter methods by way of a BeanInfo object.

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

  • Gets and displays the default value of the property named size using the jsp:getProperty tag
  • Changes the value of the property named size using the jsp:setProperty tag
  • Gets and displays the modified value using the jsp:getProperty tag

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

In this and previous lessons, I have discussed the following JSP syntax:

Output comments:
<!– comment [ <%= expression %> ] –>

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 ]” |
    “text/html ; charset=ISO-8859-1” ]
  [ isErrorPage=”true|false” ]

jsp:forward tag:
<jsp:forward page=
     “{ relativeURL | <%= expression %> }” />

jsp:useBean tag:
    {       class=”package.class” |
            type=”package.class” |
            class=”package.class” type=”package.class” |
            beanName=”{ package.class | <%= expression %> }”
    {       /> | > other tags </jsp:useBean>  }

   {  property= “*” |
      property=”propertyName” [ param=”parameterName” ] |
      property=”propertyName” value=
          “{ string | <%= expression %> }”

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

Copyright 2000, Richard G. Baldwin.  Reproduction in whole or in part in any form or medium without  express written permission from Richard Baldwin is prohibited.

About the author

Richard Baldwin is a college professor and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.

Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two.  He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas.  He is the author of Baldwin’s Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.

Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories