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).
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
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.
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!