0% found this document useful (0 votes)
20 views23 pages

Unit 5

Uploaded by

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

Unit 5

Uploaded by

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

Java Beans

JavaBeans is a portable, platform-independent model written in Java


Programming Language. Its components are referred to as beans.

JavaBeans are classes which encapsulate several objects into a single object. It
helps in accessing these object from multiple places. JavaBeans contains
several elements like Constructors, Getter/Setter Methods and much more.

JavaBeans has several conventions that should be followed:



Beans should have a default constructor (no arguments)

Beans should provide getter and setter methods
A getter method is used to read the value of a readable property
To update the value, a setter method should be called

Beans should implement java.io.serializable, as it allows to save, store and
restore the state of a JavaBean you are working on
Prepared by : Dr. Ahmad Jamal
JavaBean Properties
A JavaBean property can be accessed by the user of the object. The feature
can be of any Java data type, containing the classes that you define. It may be
of the following mode: read, write, read-only, or write-only. JavaBean features
are accessed through two methods:

1: Getters
2: Setters

Prepared by : Dr. Ahmad Jamal


Getter Example

1. getEmployeeName ()

For example, if the employee name is firstName, the method name would be
getFirstName() to read that employee name. This method is known as an
accessor. Properties of getter methods are as follows:

Must be public in nature


Return-type should not be void
The getter method should be prefixed with the word get
It should not take any argument

Prepared by : Dr. Ahmad Jamal


Setter Example

2.setEmployeeName ()

For example, if the employee name is firstName, the method name would be
setFirstName() to write that employee name. This method is known as a
mutator. Properties of setter methods:

Must be public in nature


Return-type should be void
The setter method has to be prefixed with the word set
It should take some argument

Prepared by : Dr. Ahmad Jamal


Employee.java

public class Employee implements java.io.Serializable


{
private int id;
private String name; public void setName(String name)
public Employee() {
{ this.name = name;
} }
public void setId(int id) public String getName()
{ {
this.id = id; return name;
} }
public int getId() }
{
return id;
}
Prepared by : Dr. Ahmad Jamal
Employee1.java

public class Employee1 {


public static void main(String args[])
{
Employee s = new Employee();
s.setName("Dr. Ahmad");
System.out.println(s.getName()); Output
} Dr. Ahmad
}

Prepared by : Dr. Ahmad Jamal


Application Builder Tools
When working with Java Beans, most developers use an application builder
tool, a utility that enables. you to configure a set of Beans, connect them
together, and produce a working application. Its major capabilities arc the
following:

1. A palette is provided that lists all of the available Beans. As addition Beans
are developed or purchased, they can be added to the palette.
2.A worksheet is displayed that allows the designer to lay but Beans in a
graphical user interface. A designer may drag and drop a Bean from the palette
to this worksheet.
3.Special editors and customize allow a Bean to be configured. This is the
mechanism by which the behavior of a Bean may be a particular environment.
4.Commands allow a designer to inquire about the slate ‘and behavior of a
Bean. This informational automatically becomes available when a Bean is
added to the palate Prepared by : Dr. Ahmad Jamal
5. Capabilities exist to interconnect Beans. This means that events generated
by one’ component are mapped to method invocations on other components.

7. When a collection of Beans has been configured and connected, it is


possible. to save all of this information in a persistent storage area. At a later
time, this information can then be used to restore the state of the application.

Prepared by : Dr. Ahmad Jamal


The Bean Developer Kit (BDK)

The Bean Developer Kit (BDK), available from the Java Soft site, is a simple
example of a tool that enables you to create, configure, and connect a set of
Beans. There is also a set of sample Beans with their source code.

NOTE: BDK is no longer available for download from the Java website.

Prepared by : Dr. Ahmad Jamal


Jar files
A JAR (Java Archive) is a package file format typically used to aggregate many
Java class files and associated metadata and resources (text, images, etc.) into
one file to distribute application software or libraries on the Java platform.
In simple words, a JAR file is a file that contains a compressed version of .class
files, audio files, image files, or directories. We can imagine a .jar file as a
zipped file(.zip) that is created by using WinZip software. Even, WinZip software
can be used to extract the contents of a .jar .

Syntax(Create a Jar file):


jar cf jarfilename inputfiles

Syntax(View Jar file):


jar tf jarfilename

Here, cf represents to create the file, tf represents the table view of file contents.
Prepared by : Dr. Ahmad Jamal
Syntax(Extracting a JAR file)
jar xf jarfilename

Syntax(Updating a JAR File)


jar uf jar-file input-file(s)

Here, xf represents extract files from the jar files, ‘uf’ represents the updated jar
file.

Syntax(Running a JAR file)


java -jar pack.jar

Prepared by : Dr. Ahmad Jamal


Introspection
At the core of Java Beans is introspection. This is the process of analyzing a
Bean to determine its capabilities. This is an essential feature of the Java Beans
API because it allows another application, such as a design tool, to obtain
information about a component. Without introspection, the Java Beans
technology could not operate.

Prepared by : Dr. Ahmad Jamal


Java Beans API
The Java Beans functionality is provided by a set of classes and interfaces in
the java.beans package.

Prepared by : Dr. Ahmad Jamal


Session Bean
Session bean encapsulates business logic only, it can be invoked by local,
remote and webservice client.
It can be used for calculations, database access etc.

The life cycle of session bean is maintained by the application server.

There are 3 types of session bean.


1) Stateless Session Bean: It doesn't maintain state of a client between multiple
method calls.

2) Stateful Session Bean: It maintains state of a client across multiple requests.

3) Singleton Session Bean: One instance per application, it is shared between


clients and supports concurrent access.
Prepared by : Dr. Ahmad Jamal
Entity Bean
An entity bean, in the context of Java Platform 2, Enterprise Edition (J2EE),
represents the business objects retained at the end of a session in a persistent
storage mechanism. Business objects may include items like customer name,
account number and/or account balance, etc

In J2EE, a relational database is a persistent storage mechanism. In a relational


database, there is a table for each entity bean and every bean instance
corresponds to a particular table row.

The following are characteristics differentiating entity beans from session beans:


Entity beans are retained after the end of a session, unlike session beans.

Entity beans permit shared data access.

Entity beans have a primary key or a unique identifier.
Prepared by : Dr. Ahmad Jamal
Enterprise Java Beans
Enterprise Java Beans (EJB) is one of the several Java APIs for standard
manufacture of enterprise software. EJB is a server-side software element that
summarizes business logic of an application.

Types of Enterprise Java Beans


Session Bean: Session bean contains business logic that can be invoked by
local, remote or webservice client. There are two types of session beans:
(i) Stateful session bean and (ii) Stateless session bean.
Message Driven Bean: Like Session Bean, it contains the business logic but it
is invoked by passing message.
Entity Bean: It summarizes the state that can be remained in the database. It is
deprecated. Now, it is replaced with JPA (Java Persistent API). There are two
types of entity bean:
(I) Bean Managed Persistence (II) Container Managed Persistence
Prepared by : Dr. Ahmad Jamal
Use of Enterprise Java Beans

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

Application needs to be scalable. EJB applications supports load balancing,
clustering and fail-over.

Application needs encapsulated business logic. EJB application is
differentiated from demonstration and persistent layer.

Prepared by : Dr. Ahmad Jamal


Advantages of Enterprise Java Beans
1. EJB repository yields system-level services to enterprise beans, the bean
developer can focus on solving business problems. Rather than the bean
developer, the EJB repository is responsible for system-level services such as
transaction management and security authorization.

2. The beans rather than the clients contain the application’s business logic, the
client developer can focus on the presentation of the client. The client developer
does not have to code the pattern that execute business rules or access
databases. Due to this the clients are thinner which is a benefit that is
particularly important for clients that run on small devices.

3. Enterprise Java Beans are portable elements, the application assembler can
build new applications from the beans that already exists.

Prepared by : Dr. Ahmad Jamal


Disadvantages of Enterprise Java Beans

1. Requires application server


2. Requires only java client. For other language client, you need to go for
webservice.
3. Complex to understand and develop EJB applications.

Prepared by : Dr. Ahmad Jamal


Remote Method Invocation
The RMI (Remote Method Invocation) is an API that provides a mechanism to
create distributed application in java. The RMI allows an object to invoke
methods on an object running in another JVM.

The RMI provides remote communication between the applications using two
objects stub and skeleton.

A remote object is an object whose method can be invoked from another JVM.

Prepared by : Dr. Ahmad Jamal


stub
The stub is an object, acts as a gateway for the client side. All the outgoing
requests are routed through it. It resides at the client side and represents the
remote object. When the caller invokes method on the stub object, it does the
following tasks:


It initiates a connection with remote Virtual Machine (JVM),

It writes and transmits the parameters to the remote Virtual Machine (JVM),

It waits for the result

It reads the return value or exception, and

It finally, returns the value to the caller.

Prepared by : Dr. Ahmad Jamal


skeleton
The skeleton is an object, acts as a gateway for the server side object. All the
incoming requests are routed through it. When the skeleton receives the
incoming request, it does the following tasks:


It reads the parameter for the remote
method

It invokes the method on the actual
remote object, and

It writes and transmits (marshals) the
result to the caller.

Prepared by : Dr. Ahmad Jamal


RMI Example
client-server application using RMI
Total 6 Steps needs to follow to create and run RMI application.
In the rmi application, both client and server interacts with the remote interface.
The client application invokes methods on the proxy object, RMI sends the
request to the remote JVM. The return value is sent back to the proxy object
and then to the client application.

1) create the remote interface


2) Provide the implementation of the remote interface
3) create the stub and skeleton objects using the rmic tool
4) Start the registry service by the rmiregistry tool
5) Create and run the server application
6) Create and run the client application

Prepared by : Dr. Ahmad Jamal

You might also like