SunQuest
 
       Java
  Home arrow Java arrow Page 4 - Overview of Java Web Technologies, Par...
Dev Shed Forums 
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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Overview of Java Web Technologies, Part 2 - Calling a Bean from a JSP Page


    (Page 4 of 10 )

    Before you can use a bean in your JSP page, you must make the bean available, using the jsp:useBean action element. This element has attributes that you can use to set the bean’s properties. The syntax for the jsp:useBean element has two forms:

     
    <jsp:useBean (attribute="value")+/> 

     
    <jsp:useBean (attribute="value")+> 
    initialization code 
    </jsp:useBean

    You use the first form if you do not need to write any initialization code, and the second form if you do. The (attribute=“value”)+ part of the code means that one or more attributes must be present. The five attributes that can be used in a jsp:useBean action element are as follows:

    • The id attribute defines a unique identifier for the bean. This identifier can be used throughout the page and can be thought of as the object reference for the bean. The value for the id attribute has the same requirements as a valid variable name in the current scripting language.

    • The class attribute specifies the fully qualified name for the JavaBean class. A fully qualified name is not required if the bean’s package is imported using the page directive, however.

    • If the type attribute is present in a jsp:useBean element, it specifies the type of the JavaBean class. The type of the bean could be the type of the class itself, the type of its superclass, or an interface the bean class implements. Normally, this attribute isn’t often used and you use the class attribute instead.

    • The scope attribute defines the accessibility and the life span of the bean. This attribute can take one of the following values: page, request, session, or application. The default value of the scope attribute is page. The scope attribute values control how long the bean will continue to exist, as follows:

      • page The bean is available only in the current page after the point where the jsp:useBean action element is used. A new instance of the bean will be instantiated every time the page is requested. The bean will be automatically destroyed after the JSP page loses its scope; that is, when the control moves to another page. If you use the jsp:include or jsp:forward tags, the bean will not be accessible from the included or forwarded page.

      • request The accessibility of the bean is extended to the forwarded or included page referenced by a jsp:forward or jsp:include action element. The forwarded or included page can use the bean without a jsp:useBean action element. For example, from a forwarded or included page, you can use the jsp:getProperty and jsp:setProperty action elements that reference the bean instantiated in the original page.

      • session The bean scope applies to the user’s session object. The instance of the bean will continue to exist as long as the user’s session object exists. In other words, the bean’s accessibility extends to other pages. Because the bean’s instance is put in the session object, you cannot use this scope if the page on which the bean is instantiated does not participate in the JSP container’s session management.

      • application This scope lives throughout the life of the JSP container itself. The bean is available from any resource in the application.

    • The beanName attribute represents a name for the bean that the instantiate method of the java.beans.Beans class expects.

    Either the class attribute or the type attribute must be present.

    For example, here is how you use the jsp:useBean element to instantiate a bean called ch01.LongBean:

     
    <jsp:useBean id="theBean" class="ch01.LongBean"/> 

    Alternatively, you can import the package ch01 using a page directive and refer to the class using its name, as follows:

     
    <%@ page import="ch01" %> 
    <jsp:useBean id="theBean" class="LongBean"/> 

    NOTE: The bean is available in the page after the jsp:useBean action element. It is not available before that point.

    As an example, lets go through the process of using a simple bean on a JSP page. Listing 6 shows a Java class called ch01.AdderBean.

    Listing 6 The ch01.AdderBean Class

     
    package ch01
    public 
    class AdderBean 
    public int add
    (int aint b) { 
    return 
    (b); 

    }

    After you create the AdderBean, you need to compile it to obtain the AdderBean.class file. Then you need to copy the bean class file to the classes directory under WEB-INF under your application directory. The deployment must take into account the package name. In this case, you need to create a directory called ch01. Copy your AdderBean.class file to this ch01 directory. Once you have a JavaBean ready and its class file stored in the proper directory, you can use it from a JSP page. Now, you can create a JSP page that calls the bean, as shown in Listing 7.

    Listing 7 Calling a Bean from a JSP Page (callBean.jsp)

     
    <jsp:useBean id="theBean" class="ch01.AdderBean"/> 
    <html
    <head></head
    <body
    <% 
    int x 
    4
    int y 

    int result 
    theBean.add(xy); 
    out
    .print("4+5=" result); 
    %> 
    </body
    </html

    After you create the JSP page, start or restart Tomcat and direct your browser to the URL and to the JSP page you just wrote. Your browser should display the following string:

     
    4+5=

    NOTE: You can also use a JAR for your JavaBean. In this case, you must copy the .jar file into the lib directory under the WEB-INF directory of your application directory to make it available to JSP pages in your application.

    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

    - 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
    - Gaming Development Setup





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway