java-bean
java-bean
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.
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.
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.)
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.
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.
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.
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.
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 −
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.
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 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
<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 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 Age: 10
Advantages of JavaBean