Java Programming, Lecture Notes # 764
July 2, 2000
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:
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
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.
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:
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:
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
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.
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
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.
Review
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).
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:
<jsp:setProperty
name="beanInstanceName"
{ property=
"*" |
property="propertyName" [ param="parameterName" ] |
property="propertyName" value=
"{ string | <%= expression %> }"
}
/>
Usage
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 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.
Syntax
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.
Limitations
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:
|
Output comments:
<!-- comment [ <%= expression
%> ] -->
Hidden comments:
<%-- hidden comment --%>
Declarations:
<%! declarations %>
Expressions:
<%= expression %>
Scriptlets:
<% 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:
<jsp:useBean
id="beanInstanceName"
scope="page|request|session|application"
{
class="package.class" |
type="package.class" |
class="package.class" type="package.class" |
beanName="{ package.class | <%= expression %> }"
type="package.class"
}
{
/> | > other tags </jsp:useBean> }
jsp:setProperty
<jsp:setProperty
name="beanInstanceName"
{ property=
"*" |
property="propertyName" [ param="parameterName" ] |
property="propertyName" value=
"{ string | <%= expression %> }"
}
/>
jsp:getProperty
<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.
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.
-end-