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

JSP and JavaBeans - 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).

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Although you can easily write JSP pages by interweaving HTML tags and Java code, there are at least two disadvantages to this approach. First, the resulting page lacks readability. Second, there is no separation of the presentation and business rule implementation. To write a JSP page this way, a JSP author must master both Java and page design. This is difficult because not many people are conversant in both graphic design and Java programming.

A separation of labor between a graphic designer and a Java programmer is needed to expedite the development of JSP applications. This is possible through the use of JavaBeans. In this approach, the Java programmer writes and compiles JavaBeans that incorporate all the functionality needed in an application. While the programmer is doing this, the graphic designer can work with the page design. When the JavaBeans are ready, the page designer uses tags similar to HTML to call methods and properties of the beans from the JSP page.

In fact, using beans is a common practice in JSP application development. This approach is popular because using JavaBeans introduces reusability. Rather than building your own piece of code, you can simply use what other people have written. For example, you can purchase a bean for file upload and start uploading files within 30 seconds.

NOTE:
When you are designing a JavaBean for your JSP page, reusability and modularity are of utmost importance. Therefore, it is uncommon to use a bean to send HTML tags to the Web browser, because this makes the bean customized for that page. If you need to send HTML tags customized for a page, you can use custom tags instead, as explained later in this chapter.

Really, a JavaBean is just a Java class. You do not need to extend any base class or implement any interface to create a JavaBean. To be a bean, however, a Java class must follow certain rules specified by the JavaBeans specification. For example, the bean class must have a no-argument constructor. If a Java class does not contain a constructor, the compiler will create a no-argument constructor.

Setting and Getting JavaBean Properties

Optionally, a bean can have a public method that can be used to set the value of a property. This method is called a setter method or a mutator. The method does not return any value, and its name starts with set, followed by the name of the property. A setter method has the following signature:

 
public void setPropertyName (PropertyType value

For example, the setter method for the property name operand must be named setOperand. Notice that the letter case for the property name is different. A property name starts with a lowercase letter, but the name of the setter uses an uppercase letter after set.

Optionally, a bean can also have a public method that can be called to obtain the value of a property. This method is called a getter method or a accessor, and its return type is the same as the property type. Its name must begin with get, followed by the name of the property. A getter method has the following signature:

 
public propertyType getPropertyName(); 

For example, the getter method for the property named operand is getOperand. As with the setter method syntax, the property name begins with an uppercase letter within the getter method syntax.

Both setter and getter methods are known as access methods. In JSP, the jsp:getProperty and jsp:setProperty action elements are used to invoke a getter and a setter method, respectively. You can call these methods the same way you call an ordinary method, as described in the “Accessing Bean Properties” section later in this chapter.

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
   

JAVA & J2EE ARTICLES

- 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: