Home arrow Java & J2EE arrow Page 5 - Overview of Java Web Technologies, Part 2

Accessing Bean Properties - Java

Today, Budi walks us through a refresher and brief overview of server JSP programming. Today's portion covers JavaServer Pages (JSP), with a thorough overview of JavaBeans and Tags. This excerpt comes from chapter one of JavaServer Faces Programming, by Budi Kurniawan (McGraw-Hill/Osborne, ISBN 0-07-222983-7, 2004).

  1. Overview of Java Web Technologies, Part 2
  2. Sun's Solution
  3. JSP and JavaBeans
  4. Calling a Bean from a JSP Page
  5. Accessing Bean Properties
  6. Custom Tags
  7. Developing and Using Custom Tag Libraries
  8. Writing a Tag Handler
  9. Writing and Using Tags
  10. Model 2 Architecture
By: McGraw-Hill/Osborne
Rating: starstarstarstarstar / 9
March 03, 2004

print this article



To access a property in a bean, use the jsp:getProperty and jsp:setProperty action elements. The jsp:getProperty element obtains the value of an internal variable, and the bean must provide a getter method. A jsp:getProperty element returns the property value converted into String. The return value is then automatically fed into an out.print method, so it will be displayed in the current JSP page. The syntax of the jsp:getProperty element is as follows:

<jsp:getProperty name="Bean Name" property="propertyName"/> 

The name attribute must be assigned the name of the bean instance from which the property value will be obtained. The property attribute must be assigned the name of the property.

The jsp:setProperty action element sets the value of a property. Its syntax has four forms:

<jsp:setProperty name="Bean Name" property="PropertyName" value="value"/> 
<jsp:setProperty name="Bean Name" property="PropertyName"/> 
<jsp:setProperty name="Bean Name" property="PropertyName" param="parameterName"/> 
<jsp:setProperty name="Bean Name" property="*"/> 

In this book, you will learn about and use only the first form. The name attribute is assigned the name of the bean instance available in the current JSP page. In the first form of the syntax, the property attribute is assigned the name of the property whose value
is to be set, and the value attribute is assigned the value of the property.

The following example demonstrates the use of the jsp:getProperty and jsp:setProperty action elements.

Listing 8 shows a variation of the AdderBean used in the previous example. It has a private integer called memory. (Note that the variable name starts with a lowercase m.) It also has a getter method called getMemory and a setter method named setMemory. (Note that in both access methods, memory is spelled using an uppercase M.)

Listing 8 The AdderBean with Access Methods

package ch01
class AdderBean 
private int memory

public void setMemory
(int number) { 

public int getMemory() { 
return memory

public int add
(int xint y) { 

Using the jsp:setProperty and jsp:getProperty action elements, you can set and obtain the value of memory, as demonstrated in the JSP page in Listing 9.

Listing 9 Accessing a Bean Property Using jsp:setProperty and jsp:getProperty

<jsp:useBean id="theBean" class="ch01.AdderBean"/> 
<jsp:setProperty name="theBean" property="memory" value="169"/> 
The value of memory is 
<jsp:getProperty name="theBean" property="memory"/> 

NOTE: For more information about JavaBeans, see

Buy this book now!Remember: This is part two of the first chapter of JavaServer Faces Programming, by Budi Kurniawan (McGraw-Hill/Osborne, ISBN 0-07-222983). Stay tuned for more chapters of developer books from McGraw-Hill/Osborne.
Buy this book!

>>> More Java & J2EE Articles          >>> More By McGraw-Hill/Osborne

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates


Dev Shed Tutorial Topics: