0% found this document useful (0 votes)
23 views20 pages

Unit 4 Part 1 Java Bean

Uploaded by

SAKSHI CHAUHAN
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
23 views20 pages

Unit 4 Part 1 Java Bean

Uploaded by

SAKSHI CHAUHAN
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 20

UNIT 4

Java Bean & JDBC


UNIT 4 (PART 1)
Java Bean
• A Java Bean is a reusable software component that
has been designed to be reusable in a variety of
different environments and can be manipulated
visually in a builder tool.

• It is coded according to the JavaBeans API


specifications.

• It is portable, platform- independent component


model
• Java components are known as beans.

• A Bean may be visible or invisible to end user.


Example of this is a button on a graphical user
interface.

• It is the component architecture of Java & is client


side component.
A Java Bean is a java class that should follow
following conventions:

• It should have a zero-argument constructor.

• It should be Serializable and implement


the Serializable interface.

• It should provide methods to set and get the values


of the properties, known as getter and setter
methods.
java.io.Serializable interface

• Serializable is a marker interface (has no data


member and method). It is used to "mark" java
classes so that objects of these classes may get
certain capability.
JavaBeans 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 define.
• A JavaBean property may be read, write, read only,
or write only.
• JavaBean properties are accessed through two
methods in the JavaBean's implementation class:
Method Description

getPropertyName() For example, if property name is firstName, your


method name would be getFirstName() to read that
property. This method is called accessor.

setPropertyName() For example, if property name is firstName, your


method name would be setFirstName() to write that
property. This method is called mutator.
Accessing JavaBeans- The useBean action declares a
JavaBean for use in a JSP.

Accessing JavaBeans Properties

• Along with <jsp:useBean...>, we can use


<jsp:getProperty/> action to access get methods and
<jsp:setProperty/> action to access set methods.

public type getName()


{ ...... }
public void setName(type value) { ...... }
Example:
class Employee implements java.io.Serializable
{
private String name;
public Employee()
{ }
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
}
public class Test
{
public static void main(String args[])
{
Employee e=new Employee();
e.setName("ABC”);
System.out.println(e.getName());
}
}
Advantages of Java Beans
• A Bean obtains all the benefits of Java's "write-once, run-anywhere"
paradigm.

• The properties, events, and methods of a Bean that are exposed to an


application builder tool can be controlled.

• A Bean may be designed to operate correctly in different locales,


which makes it useful in global markets.

• Configuration settings of a Bean can be saved in persistent storage and


restored at a later time.

• A Bean may register to receive events from other objects and can
generate events that are sent to other objects.
JavaBeans API

• The JavaBeans functionality is provided by a set of


classes and interfaces in the java.beans package.
Interface Description
AppletInitializer Methods in this interface are used to initialize Beans that are also applets.

BeanInfo It allows designer to specify information about events, methods & properties of a Bean.

Customizer It allows designer to provide a GUI through which a bean may be configured.

DesignMode Methods in this interface determine if a bean is executing in design mode.

ExceptionListener A method in this interface is invoked when an exception has occurred.

PropertyChangeListener A method in this interface is invoked when a bound property is changed.

PropertyEditor Objects that implement this interface allow to change & display property values.

Visibility Methods in this allow a bean to execute in environment where GUI is not available
BDK (Bean Development Kit)
• Bean Development Kit is a tool that enables to create,
configure and connect a set of Beans
• It can be used to test Java Beans without writing a code.
• The Bean Development Kit (BDK) from Sun includes a simple
example of a building environment which uses beans,
called the beanbox, and some sample beans
• It is a pure Java application whose only dependency is the Java
Development Kit (JDK) 1.1.
• The BDK provides support for the JavaBeans APIs, a test
container (the “BeanBox” to test Bean behavior), sample
Beans complete with their source code, the JavaBeans
Specification.
EJB (Enterprise Java beans)
• EJB is used to implement the Business Logic Application.
• Main use – Handling Large Database
• It is a part of J2EE specification
• It is provided by Sun Microsystems to develop secured,
robust and scalable distributed applications.
• It performs:

 life cycle management


 security
 transaction management
Phases
EJB has 2 phases-

1. Presentation Logic – HTML / JSP / Servlet


2. Business Logic - EJB (Enterprise Javabeans)
Architecture of EJB

EJB Container

Home Interface Home Interface

EJB1 1 EJB 2
Data
base

Remote Interface Remote Interface

EJBn
Architecture of EJB
Benefits of EJB
• Simplified development of large scale enterprise level
application.

• Application Server/ EJB container provides most of 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
application.

• EJB container manages life cycle of EJB instances thus


developer needs not to worry about when to
create/delete EJB objects.
Types of EJB

• Entity Enterprise Javabean


• Session EJB
• Message Driven EJB
1. Session EJB

a) Stateful EJB - Preserves the previous state. (i.e stores result of last session). Its
name keeps associated client state in its instance variables. EJB Container creates a
separate stateful session bean to process client's each request. As soon as request
scope is over, it is destroyed.

b) Stateless EJB- Do not preserves their state, always work on initial state. It is used
to do independent operations. Its name does not have any associated client state,
but it may preserve its instance state. EJB Container normally creates a pool of few
stateless bean's objects and use these objects to process client's request.

2. Message Driven EJB - Based on trigger or message & works on procedures &
functions. It is invoked by EJB container when it receives a message from queue or
topic. Message driven bean is a stateless bean and is used to do task asynchronously.

3. Entity EJB- Performs a particular task but does not preserve the data of operation.
They work on single type of data of operation. They work on single type of data.

You might also like