Java
  Home arrow Java arrow Page 5 - Overview of Java Web Technologies, Par...
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Overview of Java Web Technologies, Part 2
By: McGraw-Hill/Osborne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2004-03-03

    Table of Contents:
  • Overview of Java Web Technologies, Part 2
  • Sun's Solution
  • JSP and JavaBeans
  • Calling a Bean from a JSP Page
  • Accessing Bean Properties
  • Custom Tags
  • Developing and Using Custom Tag Libraries
  • Writing a Tag Handler
  • Writing and Using Tags
  • Model 2 Architecture

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Overview of Java Web Technologies, Part 2 - Accessing Bean Properties


    (Page 5 of 10 )

    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
    public 
    class AdderBean 
    private int memory

    public void setMemory
    (int number) { 
    memory 
    number


    public int getMemory() { 
    return memory


    public int add
    (int xint y) { 
    return 
    (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
    http://java.sun.com/docs/books/tutorial/javabeans/index.html.

    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 Articles
    More By McGraw-Hill/Osborne


     

       

    JAVA ARTICLES

    - New Object Initialization in Java
    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...
    - Java Operators
    - Primitive Data Types and Basic Language Rule...
    - Java and Object-Oriented Programming
    - Java Beginning Programming





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT