JavaServer Pages and JavaBean Components, Part II

Wednesday Jul 5th 2000 by Richard G. Baldwin

Java Programming, Lecture Notes # 764


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
JavaBean components, a broad subject

Because of the breadth of the JavaBean component aspect of JSP, I have broken this topic into several lessons.  The previous lesson (see JavaServer Pages and JavaBean Components, Part I ) taught you how to

  • Prepare a Bean for use,
  • Invoke its setter and getter methods for simple properties, and
  • Invoke ordinary exposed methods on the bean.
Digging deeper into Beans

This and subsequent lessons will continue the discussion of the use of JavaBean components with JSP.

In this lesson, I will provide some additional information on design patterns.  I will also show you how to set and get Bean property values using the jsp:setProperties and jsp:getProperties tags of the JSP engine.

JavaBean Design Patterns

JavaBean components are reusable software components.  They are designed to be integrated into a development environment (such as a Visual Builder Tool or JSP) that can take advantage of their capabilities.

Integrated at the compiled level

Furthermore, they are not intended to be integrated at the source code level making use of cut-and-past, or include methodology.  Rather, they are intended to be included at the compiled level.

Learning about the Bean

The development environment must be able to learn enough about the Bean to be able to combine it with other beans, or other Java code.

Furthermore, the development environment must be able to learn that information from the compiled version of the Bean.

Properties, methods, and events

We normally say that the development environment must be able to learn about the Bean's properties, methods, and events.

Reflection and introspection

This is accomplished using Java capabilities often referred to as low-level reflection and introspection.  (There are several lessons in my online Java tutorials discussing these topics.)

Concentrate on properties and methods

In these JSP lessons, I will concentrate on properties and methods.  Please see my online Java programming tutorials to learn about events, and also to learn more about properties and methods.

What is a property?

A Bean is an object.  A property of the Bean is a stored value, or set of values, that helps to define the current state of the Bean.

For example, the text value that comprises the caption displayed on a common GUI button is a property of the Button object.

Likewise, the size value for a frame is a property of a Frame object.

Two flavors are available

Like ice cream, properties come in two flavors (obviously, ice cream comes in more than two flavors).  The two flavors of properties are:

  • Simple
  • Indexed
A simple property is a property that can have only one value at any instant in time.

An indexed property is a property that can have multiple values accessible through a simple numeric index.

What distinguishes a property?

We also say that ordinary instance variables in an object help to define its state at any instant in time.

So, you say, "what is it that distinguishes a property from an ordinary instance variable?" -- Good question!

Properties can be bound or constrained

One of the characteristics that distinguish between properties and ordinary instance variables is that Java provides features for optionally causing a property to be bound or constrained.

Although you could do the same for ordinary instance variables, you would have to write the code from scratch because there are no special provisions for doing so.

What do I mean by bound or constrained?

Also like ice cream, either flavor of property can be delivered in three forms (analogous to ice cream cones, ice cream sodas, and ice cream sundaes).  The three constructions are:

  • Plain
  • Bound
  • Constrained
A plain property is one that doesn't have any special callback/registration attributes.  (If you are unfamiliar with callbacks and registration in Java, see my online Java tutorials.)  This is the default case.

Bound properties

A bound property is a property that has the ability to register one or more interested listeners (or observers, or whatever you choose to call them), and to notify each of them when the value of the property changes.

Constrained properties

A constrained property is one that has the ability to

  • Register one or more interested listeners,
  • Notify each of them immediately prior to a change in value of the property, and
  • Deal with the fact that any one of the listeners has the right to veto the change.
A planned change that is vetoed by a listener doesn't take place.

Design patterns

Another thing that distinguishes between properties and ordinary instance variables is the use of JavaBean design patterns.

What are JavaBean design patterns?

JavaBean design patterns make it possible for the Bean programmer to notify the development environment of the existence of properties in the Bean simply by adhering to a specific naming convention for the property's set and get methods.

These methods are often referred to as setters and getters.

Tell me more about design patterns

I won't go into design patterns in detail in this lesson (see my online Java tutorials for more information).

Basically, however, the existence of methods of the following form will notify a Beans-compatible development environment that the Bean has a simple property named size.  This will be true regardless of whether the properties are plain, bound, or constrained.

public void setSize(int inputValue){//...}
public int getSize(){//...}

The design patterns specify names that include the words set and get.  That is probably why they are often called setter and getter methods.

An alternative design pattern for boolean properties

If the type of the property is boolean, an alternative form of the getter method for a simple property named finished is shown below:

public boolean isFinished(){//...}

This is a useful, self-documenting construct for the use of a boolean property getter in a conditional clause, such as in an if statement, for example.

if( theBean.isFinished() )...

Note the change in case

Note the change in the case of the first letter of the property name when used to construct the setter and getter method signatures.  This is important when you must specify the name of a property, such as when using the jsp:setProperties tag.

There are some other special rules regarding case, but this is the most common situation.

Other design patterns

Similar design-pattern concepts are used for indexed properties, as well as for exposed ordinary methods and events.

A sample JavaBean component

Figure 1 is a sample JavaBean component that makes use of design patterns to expose one simple property and one ordinary method.

The size property

The simple property named size is exposed as a result of the inclusion of the following two methods in the class definition:

public void setSize(String size){//...}
public String getSize(){//...}

I highlighted those two method signatures in red in Figure 1 to make them easy to spot.

An ordinary method

Figure 1 also uses design patterns to expose an ordinary method with the following signature:

public String exposedMethod(){//...}

I highlighted this method signature using green in Figure 1 to make it easy to spot.

For ordinary methods, the design-pattern rule is simply that all public methods are exposed methods.

What If I Don't Want to Use Design Patterns

If you don't want to use design patterns, that is OK too.

It is not necessary to use design patterns to expose the required 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.

I will show you how to design Beans without using design patterns in a subsequent lesson

Jack and the JavaBean Stalk

This is where things start feeling like the children's story about "Jack and the JavaBean Stalk."  You remember, Jack's mother sent him to sell the cow to buy some food, but he traded the cow for a bag full of magic JavaBean components.  When he planted the Beans, they did magic things.

Our Beans, in conjunction with the JSP engine will also do some magic things, as I will illustrate in the remainder of this and the next lesson.

JSP property tags

Figure 2 is a JSP page that contains jsp:setProperty and jsp:getProperty tags to set and get values in the property named size in the bean referred to by the variable named theBean.

According to the jsp:useBean tag (shown in blue), the variable named theBean refers to the bean named BaldwinBeans.jspBeanTest001.

setSize() and getSize() not invoked directly

The important thing to note here is that the Java code in this page never directly invokes the setSize() and getSize() methods of the Bean.

Java invokes property tags instead

Rather, the Java code executes tags named jsp:setProperty and jsp:getProperty.

Property name and reference are specified

In both cases, the name of the property (size) is specified as an attribute along with the name of the variable that refers to the Bean (theBean).

Here is magic

Since the Java code doesn't invoke the setter and getter methods directly, this means that the JSP engine uses design patterns to find and execute the corresponding setSize() and getSize() methods on the Bean referred to by theBean.

Low-level reflection

Although it isn't obvious here, the JSP engine uses something referred to as low-level reflection to accomplish this task.

In the next lesson, we will see something that is even more magic commonly referred to as introspection.

The browser output

Figure 3 is a replica of the output from my browser when it is used to access the JSP page shown earlier in Figure 2 .

As you can see by examining the two figures, the jsp:getProperty tag is first used to get and display the default value of the property named size.  (An HTML font tag is wrapped around the jsp:getProperty tag to cause the value returned by the action of that tag to be displayed in red.)

Default value is Medium

The value displayed is "Medium" as defined in the declaration of the property's reference variable in Figure 1 .

Change the value of the property

Then the jsp:setProperty tag is used to change the value of the property from "Medium" to "Small".

Get and display new property value

Then, the jsp:getProperty tag is used again to get and display the new value of the property.


In this example, the JSP engine uses JavaBean design patterns along with low-level reflection to find the methods needed to set and get the values of the property.

Then it automatically invokes those methods to cause the specified action to take place.

My JSP page and my Bean code didn't provide any information to the JSP engine about how to set and get the value of the property named size (other than the use of design patterns).

The jsp:setProperty tag

I can't end this lesson without providing at least a small amount of technical information.

This tag can be used to set the value of one or more properties in a JavaBean component.  As illustrated in this lesson, the JSP engine knows how to use the set methods of the Bean to set the properties.

I will illustrate in the next lesson that the JSP engine also knows how to use introspection along with an object of a class that implements the BeanInfo interface to set the values of the properties.

Syntax of the tag

The syntax of the tag is as follows:

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


As you can see, the syntax can be rather complicated, indicating that quite a lot of flexibility is available for setting the property value.

Using HTML form parameters

There are several ways by which you can use parameters submitted via an HTML form to set the properties of a Bean.  However, an understanding of those techniques requires an understanding of HTML forms and servlets and is beyond the scope of this lesson.

Getting more information

(If you have a further interest, you should first study my online tutorials on servlets, and then go to the Sun web site to obtain more detailed information about the usage of the jsp:setProperties tag.)

What about indexed properties

Although this lesson deals only with simple properties, the jsp:setProperties tag can also be used to set the value of an indexed property in a Bean.  This involves providing a property value, which is a reference to an array.  The values stored in the array are used to set the individual values in the indexed property.

The jsp:getProperty tag

This tag gets the value of a Bean property so that you can display it in a result page.

The tag gets a Bean property value using the property's get method, converts the value of a Bean property to a String and stores it in the out object.

Can also use introspection

As you will see in the next lesson, the JSP engine can also use this tag to get the value of a property even when there aren't any get methods defined by design patterns.  In that case, the actual name of each get method is defined by an object of a class that implements the BeanInfo interface.


Relative to the jsp:setProperty tag, the syntax of this tag is pretty simple.

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

Attribute values

The beanInstanceName is the name of the variable that refers to the Bean.  In the previous example, it was theBean.

The propertyName is the name by which the Bean knows the property.  In the previous example, it was size.


According to Sun, there are a few limitations that you need to be aware of as described below:

"In JSP 1.0, <jsp:getProperty> has a few limitations you should be aware of: 
  • You cannot use <jsp:getProperty> to retrieve the values of an indexed property. 
  • You can use <jsp:getProperty> with JavaBeans components, but not with enterprise beans. You can get around this limitation by writing a JSP application in which a JSP file gets values from a Bean, and the Bean in turns calls an enterprise bean."

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.

Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved