0% found this document useful (0 votes)
1 views11 pages

6 Introduction To Java Beans

JavaBeans are reusable software components in Java that encapsulate multiple objects into a single object, facilitating easier access and maintenance. They follow specific conventions, including implementing Serializable, having a public no-arg constructor, and using private member variables with public getter and setter methods. JavaBeans are commonly used in JavaServer Pages (JSP) to integrate data and business logic, enhancing usability and flexibility in application development.

Uploaded by

Sharda Kaur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views11 pages

6 Introduction To Java Beans

JavaBeans are reusable software components in Java that encapsulate multiple objects into a single object, facilitating easier access and maintenance. They follow specific conventions, including implementing Serializable, having a public no-arg constructor, and using private member variables with public getter and setter methods. JavaBeans are commonly used in JavaServer Pages (JSP) to integrate data and business logic, enhancing usability and flexibility in application development.

Uploaded by

Sharda Kaur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Introduction to Java Beans

By
Dr. Jaspreet Singh Bajaj
Introduction to JavaBean

Java introduced the concept of JavaBean. It is a software


component that has been designed to be reusable in a variety of
environments.
In other words, JavaBeans are classes that encapsulate multiple
objects into a single object. It also helps in accessing these object
from multiple places. It contains several elements like
Constructors, getter and setter methods and much more.
In Java, JavaServer Pages provides the mechanism to interact with
the Java objects called JavaBeans. The useBean action tag is
used to instantiate and access JavaBeans within the JSP pages.
This allows the developers to encapsulate the data and business
logic in the Java classes and it can easily integrate them into the
JSP pages.
Components of Javabeans
JavaBeans are classes that encapsulate many objects into a single object (the
bean). It is a Java class that should follow the following conventions:
1. Class Definition: They are defined as public classes.

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.

3. It should have a public no-arg constructor (default constructor)

4. Data Encapsulation: They encapsulate data using private member variables


(fields) to store the object's state.

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

• It is a reusable software component. A bean


encapsulates many objects into one object so that we
can access this object from multiple places. Moreover,
it provides easy maintenance.
• Can be more user friendly and less intimidating for non
java web designers
• Using javabeans rather than java Scriplets in JSP
JavaBean Properties

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

The following are the advantages of JavaBean:


•It is portable, compact, and easy.
•The JavaBean properties and methods can be exposed to another
application.
•It provides an easiness to reuse the software components.
Disadvantages
JavaBeans are mutable. So, it cannot take advantages of
immutable objects.
Creating the setter and getter method for each property
separately may lead to the boilerplate code.
Benefits of Using Beans in Java
1.Reusability: JavaBeans are designed with reusability in mind. Once a bean is
created, it can be used across different applications.
2.Encapsulation: JavaBeans encapsulate many objects into one, hiding the
internal complexity of individual components from other parts of the program.
3.Ease of Use: Most IDEs support JavaBeans by providing features like dragging
and dropping beans into applications, which makes it easier for non-programmers
or developers to build applications.
4.Integration and Flexibility: JavaBeans can be integrated into different
environments and can communicate with other components provided these
components adhere to the Beans' conventions.
5.Customization: JavaBeans can be customized easily using property editors and
customizers.
6.Persistence: JavaBeans can be made persistent, meaning their state can be
saved to and restored from storage, without the need for the application to
understand the beans' internals. This persistence is facilitated by the Serializable
interface.
Use of JavaBeans in JSP

• 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 −

<jsp:useBean id = "bean's name" scope = "bean's scope" typeSpec/>

You might also like