6 Introduction To Java Beans
6 Introduction To Java Beans
By
Dr. Jaspreet Singh Bajaj
Introduction to JavaBean
2. Must implement Serializable (store javabean state). they often implement the
Serializable interface to allow object data to be converted into a stream of
bytes for storage or transmission.
5. All properties in java bean must be private with public getters and setter
methods. E.g. (getxxxxx() or setxxxxxx())
6. Events – Supports event handling using the Java event model, where beans can register and
respond to events.
Why use JavaBean
A JavaBean property is a named feature that can be accessed by the user of the object. The
feature can be of any Java data type, containing the classes that you define.
A JavaBean property may be read, write, read-only, or write-only. JavaBean features are
accessed through two methods in the JavaBean's implementation class:
Properties of setter Methods
1.It must be public in nature.
2.The return type a should be void.
3.The setter method uses prefix set.
4.It should take some argument.
Properties of getter Methods
5.It must be public in nature.
6.The return type should not be void.
7.The getter method uses prefix get.
8.It does not take any argument.
For Boolean properties getter method name can be prefixed with either "get" or "is".
1. getPropertyName()
For example, if the property name is firstName, the
method name would be getFirstName() to read that
property. This method is called the accessor.
2. setPropertyName()
For example, if the property name is firstName, the
method name would be setFirstName() to write that
property. This method is called the mutator.
Advantages of JavaBean
• Java Scriplets
• Include Statements
• JavaBeans
Implementation of JavaBeans using JSP
The useBean action declares a JavaBean for use in a JSP. Once declared,
the bean becomes a scripting variable that can be accessed by both
scripting elements and other custom tags used in the JSP. The full
syntax for the useBean tag is as follows −