0% found this document useful (0 votes)
9 views9 pages

java-bean

Enterprise JavaBeans (EJB) are server-side components that encapsulate business logic, supporting scalability, transaction management, and diverse client access. There are three types of session beans: stateful, stateless, and singleton, each serving different use cases. JavaBeans are Java classes that follow specific conventions for properties and methods, allowing for easy integration and reuse in applications.
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)
9 views9 pages

java-bean

Enterprise JavaBeans (EJB) are server-side components that encapsulate business logic, supporting scalability, transaction management, and diverse client access. There are three types of session beans: stateful, stateless, and singleton, each serving different use cases. JavaBeans are Java classes that follow specific conventions for properties and methods, allowing for easy integration and reuse in applications.
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/ 9

UNIT – IV

Enterprise JavaBean

An enterprise bean is a server-side component that encapsulates the business logic of an application. The
business logic is the code that fulfills the purpose of the application. In an inventory control application, for
example, the enterprise beans might implement the business logic in methods
called checkInventoryLevel and orderProduct. By invoking these methods, clients can access the inventory
services provided by the application.

When to Use Enterprise Beans:


You should consider using enterprise beans if your application has any of the following requirements:

 The application must be scalable. To accommodate a growing number of users, you may need to distribute
an application’s components across multiple machines. Not only can the enterprise beans of an application
run on different machines, but also their location will remain transparent to the clients.
 Transactions must ensure data integrity. Enterprise beans support transactions, the mechanisms that
manage the concurrent access of shared objects.
 The application will have a variety of clients. With only a few lines of code, remote clients can easily
locate enterprise beans. These clients can be thin, various, and numerous.

Types of Enterprise Beans:


Enterprise Bean Types
Enterprise Bean Purpose
Type
Session Performs a task for a client; optionally may implement a web service
Message-Driven Acts as a listener for a particular messaging type, such as the Java
Message Service API
Entity It encapsulates the state that can be persisted in the database. It is deprecated. Now, it is
replaced with JPA (Java Persistent API).
Advantages:
1. As the EJB container provides system-level services to enterprise beans, the bean developer can
concentrate on solving business problems.
2. The EJB container, rather than the bean developer, is responsible for system-level services such as
transaction management and security authorization.
3. The client developer can focus on the presentation of the client. The client developer does not have to
code the routines that implement business rules or access databases.
4. The enterprise beans are portable components, the application assembler can build new applications
from existing beans. These applications can run on any compliant Java EE server provided that they
use the standard APIs.

Disadvantages:
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
What Is a Session Bean?

A session bean encapsulates business logic that can be invoked programmatically by a client over local,
remote, or web service client views. To access an application that is deployed on the server, the client invokes
the session bean’s methods. The session bean performs work for its client, shielding it from complexity by
executing business tasks inside the server.

A session bean is not persistent. (That is, its data is not saved to a database.)

Types of Session Beans


Session beans are of three types: stateful, stateless, and singleton.

Stateful Session Beans:

The state of an object consists of the values of its instance variables. In a stateful session bean, the instance
variables represent the state of a unique client/bean session. Because the client interacts (“talks”) with its bean,
this state is often called the conversational state.

As its name suggests, a session bean is similar to an interactive session. A session bean is not shared; it can
have only one client, in the same way that an interactive session can have only one user. When the client
terminates, its session bean appears to terminate and is no longer associated with the client.

The state is retained for the duration of the client/bean session. If the client removes the bean, the session ends
and the state disappears. This transient nature of the state is not a problem, however, because when the
conversation between the client and the bean ends, there is no need to retain the state.

Stateless Session Beans:

A stateless session bean does not maintain a conversational state with the client. When a client invokes the
methods of a stateless bean, the bean’s instance variables may contain a state specific to that client but only for
the duration of the invocation. When the method is finished, the client-specific state should not be retained.
Clients may, however, change the state of instance variables in pooled stateless beans, and this state is held
over to the next invocation of the pooled stateless bean. Except during method invocation, all instances of a
stateless bean are equivalent, allowing the EJB container to assign an instance to any client. That is, the state
of a stateless session bean should apply across all clients.

Because they can support multiple clients, stateless session beans can offer better scalability for applications
that require large numbers of clients. Typically, an application requires fewer stateless session beans than
stateful session beans to support the same number of clients.

A stateless session bean can implement a web service, but a stateful session bean cannot.

Singleton Session Beans:

A singleton session bean is instantiated once per application and exists for the lifecycle of the application.
Singleton session beans are designed for circumstances in which a single enterprise bean instance is shared
across and concurrently accessed by clients.

Singleton session beans offer similar functionality to stateless session beans but differ from them in that there
is only one singleton session bean per application, as opposed to a pool of stateless session beans, any of
which may respond to a client request. Like stateless session beans, singleton session beans can implement
web service endpoints.

Singleton session beans maintain their state between client invocations but are not required to maintain their
state across server crashes or shutdowns.

Applications that use a singleton session bean may specify that the singleton should be instantiated upon
application startup, which allows the singleton to perform initialization tasks for the application. The singleton
may perform cleanup tasks on application shutdown as well, because the singleton will operate throughout the
lifecycle of the application.

When to Use Session Beans


Stateful session beans are appropriate if any of the following conditions are true.

 The bean’s state represents the interaction between the bean and a specific client.
 The bean needs to hold information about the client across method invocations.
 The bean mediates between the client and the other components of the application, presenting a simplified
view to the client.
 Behind the scenes, the bean manages the work flow of several enterprise beans.

To improve performance, you might choose a stateless session bean if it has any of these traits.

 The bean’s state has no data for a specific client.


 In a single method invocation, the bean performs a generic task for all clients. For example, you might use
a stateless session bean to send an email that confirms an online order.
 The bean implements a web service.

Singleton session beans are appropriate in the following circumstances.

 State needs to be shared across the application.


 A single enterprise bean needs to be accessed by multiple threads concurrently.
 The application needs an enterprise bean to perform tasks upon application startup and shutdown.
 The bean implements a web service.

Entity Bean:

An entity bean is a remote object that manages persistent data, performs complex business logic, potentially
uses several dependent Java objects, and can be uniquely identified by a primary key. Entity beans are
normally coarse-grained persistent objects, in that they utilize persistent data stored within several fine-grained
persistent Java objects.

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.

*. The two different types of entity bean persistence are bean-managed and container-managed. An entity bean
is persistent because it is stored in a relational database, where data exists after a session ends.
*. Each entity bean is identified by a unique object identifier, which is used by the client to locate a specific
entity bean.

*. Entity beans may be used when a bean is a business object and not a method. For example, a bank account
is a business object, whereas bank account verification is a business method. An entity beam may also be used
if a bean’s state should remain persistent.

Java Bean
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 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.
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 the 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 −

S.No. Method & Description

getPropertyName()
1
For example, if property name is firstName, your method name would be getFirstName() to read
that property. This method is called accessor.

setPropertyName()
2 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 write-only attribute will have only
a setPropertyName() method.
Creating 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 Enter.

6. Type in the command to compile the Java application to instantiate the Java bean and hit Enter.

7. You are ready to test your Java program. Type in the command to run the Java runtime launcher and
hit Enter. Observe the data from your Java bean.
JavaBeans Example:
Consider a student class with few properties −
package com.tutorialspoint;

public class StudentsBean implements java.io.Serializable {


private String firstName = null;
private String lastName = null;
private int age = 0;

public StudentsBean() {
}
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public int getAge(){
return age;
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
public void setAge(Integer age){
this.age = age;
}
}

Accessing JavaBeans
The useBean action declares a JavaBean for use in a JSP. Once declared, the bean becomes a scripting variable
that can be accessed by both scripting elements and other custom tags used in the JSP. The full syntax for the
useBean tag is as follows −
<jsp:useBean id = "bean's name" scope = "bean's scope" typeSpec/>
Here values for the scope attribute can be a page, request, session or application based on your requirement.
The value of the id attribute may be any value as a long as it is a unique name among other useBean
declarations in the same JSP.
Following example shows how to use the useBean action −
<html>
<head>
<title>useBean Example</title>
</head>

<body>
<jsp:useBean id = "date" class = "java.util.Date" />
<p>The date/time is <%= date %>
</body>
</html>
You will receive the following result − −
The date/time is Thu Sep 30 11:18:11 GST 2010

Accessing JavaBeans Properties:


Along with <jsp:useBean...> action, you can use the <jsp:getProperty/> action to access the get methods and
the <jsp:setProperty/> action to access the set methods. Here is the full syntax −
<jsp:useBean id = "id" class = "bean's class" scope = "bean's scope">
<jsp:setProperty name = "bean's id" property = "property name"
value = "value"/>
<jsp:getProperty name = "bean's id" property = "property name"/>
...........
</jsp:useBean>
The name attribute references the id of a JavaBean previously introduced to the JSP by the useBean action. The
property attribute is the name of the get or the set methods that should be invoked.
Following example shows how to access the data using the above syntax −
<html>
<head>
<title>get and set properties Example</title>
</head>

<body>
<jsp:useBean id = "students" class = "com.tutorialspoint.StudentsBean">
<jsp:setProperty name = "students" property = "firstName" value = "Zara"/>
<jsp:setProperty name = "students" property = "lastName" value = "Ali"/>
<jsp:setProperty name = "students" property = "age" value = "10"/>
</jsp:useBean>

<p>Student First Name:


<jsp:getProperty name = "students" property = "firstName"/>
</p>

<p>Student Last Name:


<jsp:getProperty name = "students" property = "lastName"/>
</p>

<p>Student Age:
<jsp:getProperty name = "students" property = "age"/>
</p>

</body>
</html>
Let us make the StudentsBean.class available in CLASSPATH. Access the above JSP. the following result
will be displayed −
Student First Name: Zara

Student Last Name: Ali

Student Age: 10

Advantages of JavaBean

o The JavaBean properties and methods can be exposed to another application.


o It provides an easiness to reuse the software components.
Disadvantages of JavaBean

o JavaBeans are mutable. So, it can't take advantages of immutable objects.


o Creating the setter and getter method for each property separately may lead to the boilerplate code. .
Java beans- development phases:
1. The ConstructionPhase
2. The BuildPhase
3. The ExecutionPhase

You might also like