0% found this document useful (0 votes)
7 views30 pages

UNIT 4 - Java Beans

The document provides an overview of JavaBeans and Enterprise Java Beans (EJB), detailing their structures, properties, and methods for creating and accessing them. It explains the differences between stateless and stateful beans, as well as the types of beans including entity beans and message-driven beans. Additionally, it outlines the requirements and scenarios for using EJB in distributed applications.

Uploaded by

rajmishrag207
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)
7 views30 pages

UNIT 4 - Java Beans

The document provides an overview of JavaBeans and Enterprise Java Beans (EJB), detailing their structures, properties, and methods for creating and accessing them. It explains the differences between stateless and stateful beans, as well as the types of beans including entity beans and message-driven beans. Additionally, it outlines the requirements and scenarios for using EJB in distributed applications.

Uploaded by

rajmishrag207
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/ 30

Ajay Kumar Garg Engineering College, Ghaziabad

(College Code: 027)

Web Technologies
Unit 4
L1
Java Beans
Ms Mili Srivastava
Assistant Professor
Department of Information Technology
06/08/2025
• UNIT-4 Enterprise Java Bean: Preparing a Class to be a JavaBeans,
Creating a JavaBeans, JavaBeans Properties, Types of beans, Stateful
Session bean, Stateless Session bean, Entity bean Java Database
Connectivity (JDBC): Merging Data from Multiple Tables: Joining,
Manipulating, Databases with JDBC, Prepared Statements,
Transaction Processing, Stored Procedures
What is Java Bean?

• A JavaBean is a specially constructed Java class written in the Java and coded
according to the JavaBeans API specifications.
• 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.
• JavaBeans is a component architecture that is platform neutral, which means that
it uses the Java capability of "Write Once, Run Anywhere" for the development of
reusable components.
• This capability allows developers to create and then use these components in
combination with an organization's existing applications.
• JavaBeans It is Java's component architecture, which allows
components built with Java to be used in graphical programming
environments.
• Graphical development environments let you configure components
by specifying aspects of their visual appearance (like the color or label
of a button) in addition to the interactions between components
(what happens when you click on a button or select a menu item).
• Following are the unique characteristics that distinguish a JavaBean
from other Java classes −
• It provides a default, no-argument constructor.
• It should be serializable and that which can implement
the Serializable interface.
• It may have a number of properties which can be read or written.
• It may have a number of "getter" and "setter" methods for the
properties.
Syntax for setter methods:
1.It should be public in nature.
2.The return-type should be void.
3.The setter method should be prefixed with set.
4.It should take some argument i.e. it should not be no-arg method.
Syntax for getter methods:
5.It should be public in nature.
6.The return-type should not be void i.e. according to our requirement
we have to give return-type.
7.The getter method should be prefixed with get.
8.It should not take any argument.
• A JavaBean property is a named S.No. Method & Description
attribute that can be accessed
by the user of the object. The
attribute can be of any Java data getPropertyName()
1 For example, if property name is firstName, your
type, including the classes that method name would be getFirstName() to
you define. read that property. This method is called accessor.
• A JavaBean property may
be read, write, read only,
or write only. JavaBean setPropertyName()
For example, if property name is firstName, your
properties are accessed through 2
method name would be setFirstName() to
two methods in the JavaBean's write that property. This method is called mutator.
implementation class −
structure of JavaBean class
// Java program to illustrate the
// structure of JavaBean class
public class TestBean {
private String name;
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
}
// Java program to illustrate the
// getName() method on boolean type attribute
public class Test {
private boolean empty;
public boolean getName()
{
return empty;
}
public boolean isempty()
{
return empty;
}
}
How to Create a Java Bean

• A Java bean is a Java class that has private member variables, public getter and setter
methods, and a zero-argument, public constructor (supplied automatically by the
compiler).
• To create a Java bean, follow these seven steps.
1. Open your text editor and create a new file that will contain the Java bean source.
Type in the following Java statements:
• The Java bean has two properties, firstName and lastName. A property is a private
• variable exposed to external programs by means of getter and setter methods.
2. Save your file as Person.java.
3. Open your text editor to create the class that will instantiate the Java bean. Type in the
following Java statements:


•The program will instantiate the Java bean and then call the setter and
getter methods of the newly created Java bean.
4. Save your file as CreateAJavaBean.java.
5. Open a command prompt and navigate to the directory containing
your new Java programs.
• Then type in the command to compile the Java bean source and hit.
6. Type in the command to compile the Java application to instantiate
the Java bean and hi.

7. You are ready to test your Java program. Type in the command to run
the Java runtime launcher and hit. Observe the data from your Java
bean.
Simple example of JavaBean class

To access the JavaBean class, we should use getter and setter methods.
//Employee.java

package mypack;
public class Employee implements java.io.Serializable{
private int id;
private String name;
public Employee(){}
public void setId(int id){this.id=id;}
public int getId(){return id;}
public void setName(String name){this.name=name;}
public String getName(){return name;}
}
How to access the JavaBean class?

package mypack;
public class Test{
public static void main(String args[]){
Employee e=new Employee();//object is created
e.setName("Arjun");//setting value to the object
System.out.println(e.getName());
}}
Preparing a Class to be a JavaBeans

• In this section you will learn more about Beans and the BeanBox by Creating a
simple Bean
• Compiling and saving the Bean into a Java Archive (JAR) file
• Loading the Bean into the ToolBox
• Dropping a Bean instance into the BeanBox
• Inspecting the Bean's properties, methods, and events
• Generating an introspection report
Writing the bean code:
Compilation of Java Bean
EJB
• EJB is an acronym for enterprise java bean. It is a specification provided by Sun Microsystems to develop
secured, robust and scalable distributed applications.
• EJB is a server-side software component that encapsulates business logic of an application. An EJB web
container provides a runtime environment for web related software components, including computer
security, Java servlet lifecycle management, transaction processing, and other web services.
• To run EJB application, you need an application server (EJB Container) such as Jboss, Glassfish, Weblogic,
Websphere etc.
It performs:
• Life cycle management,
• Security,
• Transaction management, and
• Object pooling.
• Ejb application is deployed on the server, so it is called server side component also.
EJB Architecture:
Types of Beans
Stateless

A stateless process or application can be understood in isolation. There is no stored


knowledge of or reference to past transactions. Each transaction is made as if from
scratch for the first time. Stateless applications provide one service or function and
use content delivery network (CDN), web, or print servers to process these short-
term requests.
Stateful

• Stateful applications and processes, can be returned to again and again, like online
banking or email. They’re performed with the context of previous transactions and
the current transaction may be affected by what happened during previous
transactions. For these reasons, stateful apps use the same servers each time they
process a request from a user.
• If a stateful transaction is interrupted, the context and history have been stored so
you can more or less pick up where you left off.
• Stateful apps track things like window location, setting preferences, and recent
activity. You can think of stateful transactions as an ongoing periodic conversation
with the same person.
Difference between Stateless and Stateful
Entity Bean: It summarizes the state that can remain in the database. It is
deprecated. Now, it is replaced with JPA (Java Persistent API). There are two types
of identity bean:
 Bean-Managed Persistence: In a bean-managed persistence type of entity bean,
the programmer has to write the code for database calls. It persists across
multiple sessions and multiple clients.
 Container-Managed Persistence: Container-managed persistence an enterprise
bean that persists across the database. In container-managed persistence the
container takes care of database calls.

Message Driven Bean: Like Session Bean, it contains the business logic but it is
•invoked by passing message.
When to use Enterprise Java Beans

1.Application needs Remote Access. In other words, it is distributed.


2.Application needs to be scalable. EJB applications supports load
balancing,clustering and fail-over.
3.Application needs encapsulated business logic. EJB application is
differentiated from demonstration and persistent layer.

You might also like