WEB PROGRAMMING UNIT-IV
PREPARED BY BHAVSINGH MALOTH
o Java bean is a reusable s software component which is visible and
A JavaBean is a specially constructed Java class written in the Java and coded
according to the JavaBeans API specifications.
Following are the unique characteristics that distinguish a JavaBean from other Java
It provides a default, no-argument constructor.
It should be serializable and 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.
A JavaBean property is a named attribute that can be accessed by the user of the
object. The attribute can be of any Java data type, including classes that you
A JavaBean property may be read, write, read only, or write only. JavaBean
properties are accessed through two methods in the JavaBean's implementation
For example, if property name is firstName, your method
name would be getFirstName() to read that property. This
method is called accessor.
For example, if property name is firstName, your method
name would be setFirstName() to write that property. This
method is called mutator.
A read-only attribute will have only a getPropertyName() method, and a writeonly attribute will have only a setPropertyName() method.
The properties, events, and methods of a bean that are exposed to an application
builder tool can be controlled.
ADVANTAGES OF JAVA BEANS:
A bean obtains all the benefits of java’s “write once, run anywhere” paradigm.
A bean may be designed to operate correctly in different locals, which makes it
useful in global markets.
The configuration settings of a bean can be saved in persistent storage and
restored at a later time.
A bean may register to receive events for other objects and can generate events
that are sent to other objects.
Disadvantages with JavaBeans
A class with a nullary constructor is subject to being instantiated in an invalid
state. If such a class is instantiated manually by a developer (rather than
automatically by some kind of framework), the developer might not realize that
the class has been improperly instantiated. The compiler can’t detect such a
problem, and even if it’s documented, there’s no guarantee that the developer will
see the documentation.
Having to create a getter for every property and a setter for many, most, or all of
them can lead to an immense quantity of boilerplate code.
WHY USE JAVA BEAN?
According to Java white paper, it is a reusable software component. A bean encapsulates
many objects into one object, so we can access this object from multiple places.
Moreover, it provides the easy maintenance.
Starting the BDK: To start the BDK, follow these steps:
Change to the directory c:bdkbeanbox.
Execute the batch file called “run.bat” This causes the BDK to display the three
windows. Toolbox lists all of the different beans that have included with the
BDK. Bean box provides an area to layout and connect the beans selected from
The Bean Developer Kit (BDK):
The bean Developer kit (BDK), available for the java soft site is a simple example
of a tool enables you to create, configure and connect a set of beans.
the toolbox. Properties provide the ability to configure a selected bean.
JAR files-stands for Java Archive Files.
A jar file allows you to efficiently deploy a set of classes and their associated
resources. A set of beans can be placed into one JAR file.
Jar technology makes it much easier to deliver and install software. Also, the
elements in a Jar file are compressed, which makes down loading a JAR file much
faster than separately downloading several uncompressed files.
A developer must provide a manifest file to indicate, which of the components in
a JAR file are Java Beans. An example of a manifest file is provided in the
following listing. It defines a JAR file that contains four “.gif” files and one
“.class” file. The last entry is a bean.
Ex: NAME: sunw/deco/slides/slide0.gif NAME:
Creating a JAR file:
The following command creates a Jar file named “xyz.jar” that contains all of the
“.class” and “.gif” files in the current directory
Ex: Jar cf xyz.jar *.class *.gif
If a manifest file such as xyz.mf is available, it can be used with the following
Ex: Jar cfm xyz.jar yxz.mf *.class *.gif
A quality of a bean to express the properties of itself is known as Introspection.
Introspection can be done in two ways:
Simple naming conventions
Providing additional classes to have introspection
Simple naming convention makes use of setter and getter methods.
In case of providing additional classes, for a bean class, to have the properties of
the bean, another class files are written, called for ex: Bean property class.
1.Bean Naming Conventions:
– set or modify a bean property value
retrieve property values from a bean
must start with “get”
must start with “set”
For ex: Simple naming convention bean will be like
Property1 ( )
Property2 ( )
For ex: providing additional classes will be like:
Design patterns for Properties:
A property is a subset of a beans state. Three types of properties:
Simple Properties: A simple property has a single value. I t can be identified by the
following design patterns, where N is the name of the property and T is the type.
public T getN( );
public void setN( T arg);
Boolean Properties: A Boolean property has a value of true or false. It can be identified
by the following design patterns, where N is the name of the property.
public Boolean isN( );
public Boolean getN( );
public void setN(Boolean value);
Indexed Properties: An Indexed property consists of multiple values. It can be identified
by the following design patterns, where N is the name of the property and T is the type:
public T getN (int index);
public void setN(int index, T value);
public void setN(T values);
Creating a new Bean: The steps that are involved in creating a new bean are:
1. Create a directory for the new bean.
2. Create the java source file(s).
3. Compile the source file(s).
4. Create a manifest file.
5. Generate a JAR file.
6. Start the BDK.
Creating a Bean:
Step 1: import java.awt.*; import
public class Bean1 extends Canvas implements Serializable
public Bean1 ( )
jar cfm demo.jar manifest.txt Bean1.class
Dept. of Computer Science
Open the bean box:
i. In the folder c:bdkbeanboxrun.bat
ii. “run.bat” will open the bean box tool.
File->Load->demo. jar / (or)
Copy demo.jar in beans / jars directory.
A bean class can fire off any type of event, including custom events. As with properties,
events are identified by a specific pattern of method names.
public void add<Event>Listener(<Event>Listener a)
public void remove<Event>Listener(<Event>Listener a)
The listener type must be a descendant of java.util.EventListener.
For example, a Swing JButton is a bean that fires action events when the user clicks on it.
JButton includes the following methods (actually inherited from AbstractButton), which
are the bean pattern for an event:
public void addActionListener(ActionListener l);
public void removeActionListener(ActionListener l);
Bean events are recognized by builder tools and can be used in wiring components
together. For example, you can wire a button's action event to make something happen,
like invoking another bean's method.
The JavaBeans specification provides for user-defined customizers, through
which you can define a higher level of customization for bean properties than is
a available with property editors.
When you use a bean Customizer, you have complete control over how to
configure or edit a bean. A Customizer is an application that specifically targets a
bean’s customization. Sometimes properties are insufficient for representing a
bean's configurable attributes.
Customizers are used where sophisticated instructions would be needed to change
a bean, and where property editors are too primitive to achieve bean
All customizers must:
Implement the java.beans.Customizer interface This means implementing
methods to register PropertyChangeListener objects, and firing property change
events at those listeners when a change to the target bean has occurred.
Extend java.awt.Component or one of its subclasses.
Implement a default constructor. Associate the customizer with its target class via
The JavaBeans functionality is provided by a set of classes and interfaces in the
Methods in this interface are used to initialize Beans that are
This interface allows the designer to specify information about
the events, methods and properties of a Bean.
This interface allows the designer to provide a graphical user
interface through which a bean may be configured.
Methods in this interface determine if a bean is executing in
A method in this interface is invoked when an exception has
A method in this interface is invoked when a bound property is
Objects that implement this interface allow the designer to
change and display property values.
A method in this interface is invoked when a Constrained
property is changed.
Methods in this interface allow a bean to execute in
environments where the GUI is not available.
JAVABEAN AP I:
The JavaBeans API architecture supplies a set of classes and interfaces to provide
The following figure represents a hierarchy of the FeatureDescriptor classes:
Enterprise Java Beans (EJB) is development architecture for building highly
scalable and robust enterprise level applications to be deployed on J2EE
compliant Application Server such as JBOSS, Web Logic etc.
EJB 3.0 is being a great shift from EJB 2.0 and makes development of EJB based
applications quite easy.
EJB stands for Enterprise Java Beans. EJB is an essential part of a J2EE platform.
J2EE platform have component based architecture to provide multi-tiered,
distributed and highly transactional features to enterprise level applications.
EJB provides an architecture to develop and deploy component based enterprise
applications considering robustness, high scalability and high performance. An
EJB application can be deployed on any of the application server compliant with
J2EE 1.3 standard specification. We'll be discussing EJB 3.0 in this tutorial.
An EJB is just a collection of Java classes and XML file, bundled into a single
unit. The Java classes must follow certain rules and provide certain callback
EJB is just a specification. It is not a product.
EJBs are reusable components.
EJB is a widely-adopted server-side component architecture for J2EE.
BENEFITS (OR) ADVANTAGES OF EJB
EJB components are designed to encapsulate business logic, and to protect the
application developer from having to worry about system level issues.
Application Server/ EJB container provides most of the system level services like
transaction handling, logging, load balancing, persistence mechanism, exception
handling and so on. Developer has to focus only on business logic of the
EJB container manages life cycle of ejb instances thus developer needs not to
worry about when to create/delete ejb objects.
EJB components are server-side components written entirely in the Java
EJB components contain business logic only - no System-level programming
System-level services (i.e. "plumbing") such as transactions, security, Life-cycle,
threading, persistence, etc. are automatically managed for the EJB component by
the EJB server
EJB architecture is inherently transactional, distributed, portable, multi-tier,
scalable and secure
EJB components are fully portable across any EJB server and any OS, work with
Components are declaratively customized
There are four major parts to every bean: the home interface, the remote interface,
the implementation class and the XML deployment descriptor
Simplified development of large scale enterprise level application.
EJB VS JAVABEANS
The JavaBeans architecture is meant to provide a format for general-purpose
components whereas the EJB architecture provides a format for encapsulation
and management of business logic.
JavaBeans has tier of execution at Client and EJB has at Server (specifically
business logic tier)
1. Session Beans
VAR IETIES OF BEANS :
In JavaBeans the runtime execution environment provides services like Java
libraries, Java application etc. The EJB runtime environment provides services
of Persistence, declarative transactions and security, connection pooling and
Stateful session bean
Stateless session bean
2. Entity Beans
With container-managed persistence
With bean-managed persistence
3. Message-Driven Beans
EJB are primarily of three types which are briefly described below:
Session bean stores data of a particular user for a single session. It can
be stateful or stateless. It is less resource intensive as compared to entity beans.
Session bean gets destroyed as soon as user session terminates.
Entity beans represents persistent data storage. User data can be saved to
database via entity beans and later on can be retrived from the database in the
Message Driven Bean
Message driven beans are used in context of JMS (Java Messaging Service).
Message Driven Beans can consumes JMS messages from external entities and
STATELESS SESS ION BE AN LIFE CYCLE
1. There are two stages in the Lifecycle of Stateless Session Bean. These are:
3. b) Method Ready Pool
In the Method Ready Pool stage bean has instance(s) in the memory of the EJB
container and it is ready to serve clients. On the startup of the EJB container some
instances of the bean are created and placed in the pool. EJB container creates the
new instance of the Bean and then sets the session context (setSessioncontext())
and it calls the ejbCreate() method to place the bean in the Method Ready Pool
2. a) Does Not Exist
In the Does Not Exist stage, bean does not have instance in the memory. In this
stage bean has not been instantiated.
stage. Container calls the ejbRemove() method to move the bean into Does Not
4. Following Diagram shows the Life cycle of Stateless Session Bean
STATEFUL SESS ION BEAN LIFE CYCLE
There are there stages in the life cycle of Stateful Session bean Life cycle. These are:
a) Does Not Exist
This is the Does Not Exist stage, bean does not have instance in the memory. In this stage
bean has not been instantiated.
b) Method Ready Pool
In the Method Ready Pool stage bean has instance in the memory of the EJB container
and it is ready to serve client. One instance of the Stateful Session Bean servers only one
client. When Client Calls create(args) method on the Home Interface, server creates new
c) Passive state
In the Passive state the bean is passivated to conserve the resource. The passivate method
is called before the instance enters the "passive" state. The instance should release any
resources that it can re-acquire later in the ejbActivate() method. After the passivate
method completes, the instance must be in a state that allows the container to use the Java
Serialization protocol to externalize and store away the instance's state. ejbRemove or
Timeout moves the bean into Does Not Exist stage.
instance of the bean and sets the Session Context and then container calls the
ejbCreate(args) method on the bean and places the bean into Method Ready Pool stage.
ejbRemove or Timeout moves the bean into Does Not Exist stage.
Following Diagram shows the Life cycle of Statelful Session Bean
A requirement of any of the services provided by an EJB container like
transactions, scalability, persistence, security, future growth possibilities is an
appropriate reason to use EJB in the design of the application.
WHY USE EJBS IN YOUR DES IGN?