Learning Center
Plans & pricing Sign in
Sign Out

Java Web Technology unit-3



                                        JAVA BEANS
Definition:-A JavaBeans is software reusable component. Components are self-contained,
reusable software units that can be visualized by a builder tool (BDK).These JavaBeans
components are known as Bean. A JavaBeans is a simple Java class (a POJO class) that can be
used in simple and complex calculations in real-time applications. It may be visible as a GUI (act
as a Button) or invisible (a stream of multimedia information).


1) A Bean obtains all the benefits of Java's "write-once, run-anywhere" paradigm.
2) The properties, events, and methods of a Bean that are exposed to an application Builder tool
can be controlled.
3) A Bean may be designed to operate correctly in different locales, which makes it Useful in
global markets.
4) Auxiliary software can be provided to help a person configure a Bean. This software is only
needed when the design-time parameters for that component are being set. It does not need to be
included in the run-time environment.
5) The configuration settings of a Bean can be saved in persistent storage and restored at a later
6) A Bean may register to receive events from other objects and can generate events that are sent
to other objects.

                      The following Conventions to create a JavaBeans.

1) It should have any default (no-argument) constructor.

2) It should consists only setters/getters and other methods.

 The setter Method starts with set followed by the name of the property.
e.g. setUserName (String name). It should have only void as return type.

                       Syntax: public void setUserName (String name)

The getter method starts with get followed by the name of the property.
E.g. getUserName().It should have proper return type and name of the method.

                             Syntax: public String getUserName()

Note: Always store a JavaBeans class in a Package.

                                   A simple Bean program

package feroz.beans;
public class MyBean
  String name;
  public void setName(String name)
  public String getName()
   return this. name;
                                 //Testing MyBean…….

import feroz.beans.MyBean;
public class TestMyBean
   public static void main(String args[])
         MyBean mb = new MyBean();
         mb.setName(“Md Feroz”);
         System.out.println(“MY NAME IS”+mb.getName());

                              BEAN DEVELOPER KIT (BDK)

The Bean Developer Kit (BDK) is a simple development tool that enables to create, configure,
and connect a set of JavaBeans.
Installation: A BDK( file is downloaded from the website and extract in c:>drive
the extraction would create the directory structure, which consists sample examples and the
folder name beans. This consists beanbox sub folder. Goto the beanbox folder double click on
run-bat for windows.

Then a BDK Builder Starts up.


                                         Juggler Bean

ToolBox : This window displays all the samples beans provided by BDK.
BeanBox : This window is used to test the capability and functionality of a bean.
Properties: This is used to configure the selected bean, such as configuring the background
color, font, focusCycleRoot etc.
Method Tracer : This window is used for debugging purpose

How to Use BDK

1) left click on one of the Beans in the ToolBox(E.g.”Juggler”Bean).Your Mouse cursor would

2) Move the cursor to a position on the Bean Box window where your selected bean is displayed.

3) For Juggler bean the properties window shows the properties such as animation Rate as show
in above figure.

Developing a Simple JavaBeans using a BDK


The following are the steps to create a Bean after installing the BDK tool.

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.
7. Test.

JAR Files: A jar file is a java archive file which consists a set of classes and other resources that
allows a JavaBeans to deploy efficiently. JAR technology makes it much easier to deliver and
install software. Also, the Elements in a JAR file are compressed, which makes downloading a
JAR file much faster than separately downloading several uncompressed files.

Manifest Files: A developer must provide a manifest file to indicate which of the components
in a JAR file are Java Beans. i.e. Information related to JAR file, which consists .class files and
.gif files.

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: If a manifest file such as is
available, it can be used with the following command:
                          command : jar cfm xyz.jar *.class *.gif


Definition: Introspection is the process of analyzing a Bean to determine its capabilities. It asks
the JavaBeans Component what properties and events it supports. It enables the application
builder tool to find out the information about the structure and functionality of a Bean by two
ways. First by simply naming convention, second explicitly additional classes supply the
information. (i.e.) SimpleBeanInfo class.
                                 ****Design Patterns for Properties

A property is a subset of a Bean’s state. There are three types of properties.

1) Simple, 2) Boolean, and 3) indexed.

1) A Simple Properties: A simple property has a single value. It can be identified by the
following design patterns, where N is the name of the property and T is its type.

                              public T getN( );
                              public void setN(T arg);

In simple a setters/getters design properties.

public class MyBean
  String name;
  public void setName(String name)
  public String getName()

2) 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);

either the first or second pattern can be used to retrieve the value of a Boolean property.
However, if a class has both of these methods, the first pattern is used.

The following listing shows a class that has one Boolean property:

public class Line
   private boolean dotted = false;
   public boolean isDotted( )
      return dotted;
  public void setDotted(boolean dotted)

        this.dotted = dotted;

3) 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 its type:

                          public T getN(int index);
                          public void setN(int index, T value);
                          public T[ ] getN( );
                          public void setN(T values[ ]);

4) Design Patterns for Events : Beans can generate events and send them to other objects.
These can be identified by the following design patterns, where T is the type of the event:

               public void addTListener(TListener eventListener);
               public void addTListener(TListener eventListener) throws
               public void removeTListener(TListener eventListener);

Example for Simple Bean properties (First approach)

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
public class SimpleBean
    private String name;
    pribate Boolean functional;
public void setName(String name)
public String getName()
public void setFunctional(Boolean functional)
public Boolean isFunctional()

     return this.functional;

public static void main(String args[])
     BeanInfo info = Introspector.getBeanInfo(SimpleBean.class);
     System.out.println(“\n Properties derived from Bean Info Objects are :”);
     for(PropertyDescriptor pd:info.getPropertyDescriptors())
   }catch(Exception e){}
output: class, name, functional

                               //second approach (using BeanInfo interface)

BeanInfo Interface: This is a second explicitly approach to expose the bean properties, events,
and methods .This Interface includes methods as follows:

                 PropertyDescriptor[] getPropertyDescriptors();
                 MethodDescriptor[] getMethodDescriptors();
                 EventsetDescriptor[] getEventSetDescriptor();

SimpleBeanInfo is a class that provides default implementations of the BeanInfo interface,
including the three methods. By using BeanInfo class you can expose those features as you want,
provide more descriptive name, additional information about a bean.

package simran.beans;
import java.beans.*;
public class ExplicitButtonBeanInfo extends SimpleBeanInfo
        public PropertyDescriptor[] getPropertyDescriptor()
           PropertyDescriptor background = new
           PropertyDescriptor foreground = new

                    catch(IntrospectionException e){}


           A Bean that has a bound property generates an event when the property is changed. The
event is of type PropertyChangeEvent and is sent to objects that previously registered an
interest       in       receiving     such     notifications.   Bound     properties    supports
java.beans.propertyChangelistener so as to receive PropertyChageEvent notifications. You
need to provide the event listener registrations methods for PropertyChangelistener classes and
fire PropertyChangeEvent event to the            PropertyChangelistener Objects by calling their
PropertyChange methods. The java.beans.PropertyChangeSupport class enables your bean to
implement these methods.

                      Steps to create a JavaBean that supports Bound Properties

1.Import the java.beans.PropertyChangeSupport class
2.Instantiate the PropertyChangeSupport Object.
3.Implement methods to maintain the property change listeners list.
4.Modify a property set methods to fire a propertyChange event when the property is changed.

import java.beans.*;
import java.beans.PropertyChangeSupport; //setp---1
public class SimpleBeanBoundProp implements
        private String name;
        private Boolean functional;
        private PropertyChangeSupport pcs;
        public SimpleBeanBoundProp()
                 pcs = new SimpleBeanBoundProp(this); //step---2
        public void addPropertyChangeListener(PropertyChangeListener pcl)

          public void removePropertyChangeListener(PropertyChangeListener pcl)
          public void setName(String newName)
      public String getName()
      public void setFunctional(Boolean functional)
    public Boolean isFunctional()
          return this.functional;


          A Bean that has a constrained property generates an event when an attempt is made to
change its value. The event is of type PropertyChangeEvent. It is sent to objects that previously
registered an interest in receiving such notifications. Those other objects have the ability to veto
the proposed change. This capability allows a Bean to operate differently according to its run-
time environment.


          Persistence means storing the data in a permanent storage space so that it can be
accessed at a later time by the same application. A class must implements in
order to persists its configuration settings permanently.In The context of JavaBeans the BDK
tool is used to save and restore the bean configuration settings.

Saving the Beans in the BDK: start the Bean. Choose the” File “and click save option.

Restoring the Bean in the BDK: start the BDK choose “File” and then the “Load” option then
choose the “Open” dialog box. Which provides a file to be restored and click OK then the Bean
is Loaded and Functioning


        Customizers are GUI Components that contains property editors for each of the
properties of the bean. Instead of a single property sheet. Hence a Beans can be accompanied by
a customizer class that controls the customization of the Bean.
Note : All customizers must implement java.beans.Customizer interface.


        Enterprise Java Beans (EJB) is a server-side, reusable component model for distributed
applications that facilitates centrally-managed business logic and declarative deployment. An
EJB server is responsible for making the component a distributed object and for managing
services such as transactions, persistence, concurrency, and security. Enterprise JavaBeans
defines a server-side component model that allows business objects to be developed and moved
from one brand of EJB container to another.EJB components are executed in EJB containers


                     Remot                                        DB

                      JNDI          Container



Explanations: Clients of an enterprise bean can be a traditional Java application, an applet, a
JSP page or servlet, another EJB bean, or others. Note that a client never invokes a bean's
methods directly. Communication between clients and beans is done through the EJB container.

The Client must obtain the instance of a Bean by implementing the two interfaces i.e. Home and
Remote interfaces. The Home interface performs the life cycle operation of a Bean. A home
interface must extend the EJBHome interface in the javax.ejb package.
        The remote interface duplicates all the business methods that are to be available to a
client. Because the client cannot access the bean directly. A remote interface must extend the
EJBObject interface in the javax.ejb package.
Note: An instance of a remote interface is often called the EJBobject. Once a client obtains the
EJBObject it can access the Bean via JNDI. The JNDI provides the Naming Services (DNS)
which maps the domain names with IP address. If a client application wants to call an enterprise
bean, it simply needs to supply the name of the bean. JNDI will find the bean for the client.

Bean: It a Java class written by EJB developer called EJB component always lie in container
called EJB container. EJB mainly consists the Business and data logic which interacts with
database. There are mainly three types of EJB.

   1. Session Bean: A session bean implements the javax.ejb.SessionBean interface. It
        maintains the session between the clients for managing processes or tasks. For example
        transaction for some information on the web browser. Session beans represent the
        activities which exists for a short period of time. It does not represent anything in the
        database but it can access the database. sessions are client specific.

   2. Entity Bean: An entity bean implements the javax.ejb.EntityBean interface. Entity
        beans are used for modeling business concepts. Therefore it may live longer than a client
        and persists the data shared among multiple clients. For example entity bean can
        represent the bank account or a customer.

   3.   Message-Driven Bean: It is similar to session but it provides asynchronous message
        services to the clients through this components. It acts as JMS.It doesn’t retain data or
        state for a specific client but it can process messages from multiple clients.

The following are the EJB container services to EJB component
    Transaction Management.
    Data persistence
    Concurrency Control
    Security
    Connection Pooling
    Instance Pooling
    Life Cycle management of Bean
    Java Message Service (JMS)
    Remote Access to the Bean.

EJB provide a framework for the components that can be plugged into server,which enhances the
functionality of the server. EJB component can be created, controlled and managed by its
container J2EE. EJB container provides memory and database connections for multiple EJBs
components. It provides a technique for EJB to separate business logic from the database.


To top