Note You may encounter the term bean as you read about JavaBeans, here and throughout Java literature. A bean is an instance of a class created as a JavaBean. In some circles, a bean may mean an Enterprise JavaBean, too. In this book, bean refers to an instance of a JavaBean class and enterprise bean refers to an instance of an Enterprise JavaBean. Professional Java development environments use the Java Programming Language’s inherent introspection feature to peek inside JavaBeans, enabling the Java developer to access and change the properties of JavaBeans by using a GUI. The environments use structures akin to property sheets to get and set bean property values. You must follow a convention, which you may already know, when naming your accessor methods. This convention is explained later in this chapter in the section Coding JavaBean Property Accessor Methods. It s important to note that a Java developer can use a JavaBean without knowing anything about the bean s internals; the entire state of the bean is described through its properties, which must be accessible through the bean s accessor methods. After all, that s what writing component software is all about. Another requirement of all JavaBeans is that they implement either the Serializable interface or the Externalizable interface. This requirement allows a bean to be persistent, an attribute inherent to all objects that implement either of these interfaces. In this book we will only consider the Serializable interface. You may be muttering to yourself, Great! I have to become a JavaBean maven in order to use JavaBeans in JavaServer Pages. Don t worry! Everything you need to know about using JavaBeans in your JSPs is in the preceding brief definition. If you create a public class with a zero-argument constructor and no public instance variables, you can use this class in your JSP pages. If you want to save persistent data, you can write this data to a database or implement Serializable. Rather than coding blocks of scriptlets or methods in your JSPs, you can code JavaBeans containing the scriptlet or method code and invoke the methods from the bean in your JSPs. Now, that s not so bad, is it? Before you read about how to code your JSPs to use JavaBeans, a few words about coding JavaBeans are in order. Coding JavaBeans Basically, coding JavaBeans resembles coding any Java class; remember to code your bean public, write (or enable Java to automatically create) a zero-argument constructor, define no public instance variables, and use getand set methods to provide access to your nonpublic instance variables. You may already know how to code a public Java class and use a zero-argument class constructor. Whether this is the case or not, here s an example of a public class declaration with a zero-argument constructor: public class SomeClassName { //Here s the no arg constructor public SomeClassName() { } //Rest of the code for this class follows } You can give your instance variables any visibility except public, although most of the time you may opt for private visibility for your instance variables. Writing a method in a JavaBean is exactly the same as writing a method in any Java class. Code your methods as you would for any Java class, using the familiar Java language constructs you ve come to know and love. Coding JavaBean Property Accessor Methods As previously mentioned, you must follow a naming convention when coding accessor methods to read values from and write values to instance variables. JavaBean tools follow the naming convention when looking inside the bean. These tools then extract the bean properties, enabling the bean user to change the state of the bean by changing the bean properties through a property sheet.
Find the right website host for you, offering a directory of website hosts, free domain name registration tool, hosting reviews, web hosting ratings and articles for beginners, and tons of other resources, check web hosting ratings for more information.