0% found this document useful (0 votes)
3 views7 pages

Enterprise Java Beans

Enterprise Java Beans (EJB) is a Java API designed for the development of enterprise software, providing server-side business logic management and system-level services like transaction management and security. It includes three types of beans: Session Beans, Message Driven Beans, and Entity Beans, with specific use cases and advantages such as remote access and scalability. EJBs differ from JavaBeans in that they are server-side components that handle business logic, while JavaBeans are reusable components designed for client-side applications.

Uploaded by

SACHIN VERMA
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
3 views7 pages

Enterprise Java Beans

Enterprise Java Beans (EJB) is a Java API designed for the development of enterprise software, providing server-side business logic management and system-level services like transaction management and security. It includes three types of beans: Session Beans, Message Driven Beans, and Entity Beans, with specific use cases and advantages such as remote access and scalability. EJBs differ from JavaBeans in that they are server-side components that handle business logic, while JavaBeans are reusable components designed for client-side applications.

Uploaded by

SACHIN VERMA
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 7

Enterprise Java Beans (EJB) is one of the several Java APIs for standard manufacture of enterprise

software. EJB is a server-side software element that summarizes business logic of an application.
Enterprise Java Beans web repository yields a runtime domain for web related software elements
including computer reliability, Java Servlet Lifecycle (JSL) management, transaction procedure and
other web services. The EJB enumeration is a subset of the Java EE enumeration.
The EJB enumeration was originally developed by IBM in 1997 and later adopted by Sun
Microsystems in 1999 and enhanced under the Java Community Process.
The EJB enumeration aims to provide a standard way to implement the server-side business software
typically found in enterprise applications. Such machine code addresses the same types of problems,
and solutions to these problems are often repeatedly re-implemented by programmers. Enterprise Java
Beans is assumed to manage such common concerns as endurance, transactional probity and security in
a standard way that leaves programmers free to focus on the particular parts of the enterprise software
at hand.
To run EJB application we need an application server (EJB Container) such as Jboss, Glassfish,
Weblogic, Websphere etc. It performs:
1. Life cycle management
2. Security
3. Transaction management
4. Object pooling

Types of Enterprise Java Beans


There are three types of EJB:
1. Session Bean: Session bean contains business logic that can be invoked by local, remote or
webservice client. There are two types of session beans: (i) Stateful session bean and (ii) Stateless
session bean.

•(i) Stateful Session bean :


Stateful session bean performs business task with the help of a state. Stateful session bean can be used
to access various method calls by storing the information in an instance variable. Some of the
applications require information to be stored across separate method calls. In a shopping site, the items
chosen by a customer must be stored as data is an example of stateful session bean.

•(ii) Stateless Session bean :


Stateless session bean implement business logic without having a persistent storage mechanism, such as
a state or database and can used shared data. Stateless session bean can be used in situations where
information is not required to used across call methods.

2. Message Driven Bean: Like Session Bean, it contains the business logic but it is invoked by passing
message.
3. Entity Bean: It summarizes the state that can be remained in the database. It is deprecated. Now, it is
replaced with JPA (Java Persistent API). There are two types of entity bean:

•(i) Bean Managed Persistence :


In a bean managed persistence type of entity bean, the programmer has to write the code for database
calls. It persists across multiple sessions and multiple clients.

•(ii) Container Managed Persistence :


Container managed persistence are enterprise bean that persists across database. In container managed
persistence the container take care of database calls.

When to use Enterprise Java Beans


1.Application needs Remote Access. In other words, it is distributed.
2.Application needs to be scalable. EJB applications supports load balancing, clustering and fail-over.
3.Application needs encapsulated business logic. EJB application is differentiated from demonstration
and persistent layer.

Advantages of Enterprise Java Beans


1. EJB repository yields system-level services to enterprise beans, the bean developer can focus on
solving business problems. Rather than the bean developer, the EJB repository is responsible for
system-level services such as transaction management and security authorization.
2. The beans rather than the clients contain the application’s business logic, the client developer can
focus on the presentation of the client. The client developer does not have to code the pattern that
execute business rules or access databases. Due to this the clients are thinner which is a benefit that is
particularly important for clients that run on small devices.
3. Enterprise Java Beans are portable elements, the application assembler can build new applications
from the beans that already exists.

Disadvantages of Enterprise Java Beans


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.

Difference between Javabeans and Enterprise Javabeans


JavaBeans: JavaBeans are recyclable software components for Java which by using a builder tool can
be manipulated visually in line to a specific convention. JavaBeans create Java Components that can be
composed together into applets and applications.For passing as a single bean object instead of as
multiple respective objects, they are used to encapsulate many objects into a single object(the bean).
Enterprise JavaBeans(EJBs): EJBS are server-side programs which generally implement middle layer
business performance. Enterprise JavaBeans are intended to handle such frequent concerns as
perseverance, transactional integrity and security in a 3-way model, which gives programmer freedom
to focus on the specific problem in hand. It tightly couples with COBRA, COBRA-HOP compatibility
by the EJB-to-COBRA mapping. The difference between JavaBeans and Enterprise JavaBeans(EJBs)
are as following:

S.No. JavaBeans Enterprise JavaBeans

Even though EJB is a component


Javabeans is a component technology technology, it neither reconstructs nor
1.
to create universal Java components. enhances the original JavaBean
specification.

No perception of Beaninfo classes,


Beaninfo classes, property editors or property editors or customizers is in
2. customizers can be present in Enterprise JavaBeans and no additional
Javabeans. information is provided except that
described in the deployment descriptor.

An external interface called a


A deployment descriptor is given in
properties interface is given in
Enterprise JavaBeans to interpret the
3. JavaBeans, that allows a builder tool
functionality to an external builder tool or
to describes the functionality of a
IDE.
bean.
S.No. JavaBeans Enterprise JavaBeans

Enterprise JavaBeans are categorized into


There is no further category of Java
4. two types – session beans and entity
Beans.
beans.

EJBs may be transactional and


JavaBeans have not any definite
5. transactional support is provided by the
support exists for transactions.
EJB servers.

JavaBeans are designed for a single EJBs are remotely executable components
6.
process and are localized or business objects.

JavaBeans may be visible or non-


visible components.Visual GUI
7. An EJB is a non-visual isolated object.
component (Button, listbox, graphics)
are examples of JavaBeans.

JavaBeans has components bridges.A ActiveX controls are designed for the
8. JavaBeans can also be arranged as an desktop so EJB cannot be deployed as an
ActiveX control. ActiveX control.

JavaBeans are mainly designed to run


EJBs are deployed on the server-side
9. on the client side whereas one can
only.
develop server side JavaBeans.

JavaBean
In any programming language, reusability is the main concern. To achieve the same concern, Java
introduced the concept of JavaBean. It is a software component that has been designed to be reusable in a
variety of environments. In this section, we will dive into the topic and understand the horizons of
concept in this what is JavaBeans, its advantages, disadvantages, Life cycle.

In other words, JavaBeans are classes that encapsulate multiple objects into a single object. It also helps
in accessing these object from multiple places. It contains several elements like Constructors, getter and
setter methods and much more.

A JavaBean is a Java class that should follow the following conventions:

Class Definition: They are defined as public classes.


Serializability (Optional): While not mandatory, they often implement the Serializable interface to allow
object data to be converted into a stream of bytes for storage or transmission.
No-argument Constructor: JavaBeans must have a public constructor that takes no arguments (often
called a no-arg constructor). It simplifies object creation.
Data Encapsulation: They encapsulate data using private member variables (fields) to store the object's
state.
Getter and Setter Methods: For each private field, there should be a corresponding public getter and setter
method:
Getter methods (usually prefixed with get or is for booleans) retrieve the value of a field.
Setter methods (usually prefixed with set) modify the value of a field.
Why use JavaBean?
According to Java white paper, it is a reusable software component. A bean encapsulates many objects
into one object so that we can access this object from multiple places. Moreover, it provides easy
maintenance.

Simple Example of JavaBean Class


File Name: Employee.java

//Employee.java

package mypack;
public class Employee implements java.io.Serializable{
private int id;
private String name;
public Employee(){}
public void setId(int id){this.id=id;}
public int getId(){return id;}
public void setName(String name){this.name=name;}
public String getName(){return name;}
}
How to access the JavaBean class?
To access the JavaBean class, we should use getter and setter methods.

File Name: Test.java

package mypack;
public class Test{
public static void main(String args[]){
Employee e=new Employee();//object is created
e.setName("Arjun");//setting value to the object
System.out.println(e.getName());
}}
JavaBean Properties
A JavaBean property is a named feature that can be accessed by the user of the object. The feature can be
of any Java data type, containing the classes that you define.

A JavaBean property may be read, write, read-only, or write-only. JavaBean features are accessed
through two methods in the JavaBean's implementation class:

Properties of setter Methods


It must be public in nature.
The return type a should be void.
The setter method uses prefix set.
It should take some argument.
Properties of getter Methods
It must be public in nature.
The return type should not be void.
The getter method uses prefix get.
It does not take any argument.
For Boolean properties getter method name can be prefixed with either "get" or "is". But recommended to
use "is".
1. getPropertyName()
For example, if the property name is firstName, the method name would be getFirstName() to read that
property. This method is called the accessor.

File Name: PropertyNameFinder.java

1. import java.lang.reflect.Field;
2. // Class to find the property name of an object that holds a given value
3. public class PropertyNameFinder {
4. // Method to return the name of the property whose value matches the given value
5. public static String getPropertyName(Object obj, Object value) {
6. // Loop through all declared fields of the object's class
7. for (Field field : obj.getClass().getDeclaredFields()) {
8. // Make the field accessible, allowing access to private fields
9. field.setAccessible(true);
10. try {
11. // Check if the current field's value is equal to the provided value
12. if (field.get(obj).equals(value)) {
13. // Return the name of the field if the value matches
14. return field.getName();
15. }
16. } catch (IllegalAccessException e) {
17. // Handle the exception if the field is not accessible
18. System.out.println("Access to the field was denied.");
19. } catch (NullPointerException e) {
20. // Handle the case where the field's value is null to avoid crashing the program
21. System.out.println("A field value was null.");
22. }
23. }
24. // Return null if no matching field is found
25. return null;
26. }
27. // Main method to test the getPropertyName method
28. public static void main(String[] args) {
29. // Create an instance of Person
30. Person person = new Person("John Doe", 30);
31. // Try to find the field name that has the value "John Doe"
32. String fieldName = getPropertyName(person, "John Doe");
33. // Print the result, expected to be "name"
34. System.out.println(fieldName); // Outputs: name
35. // Try to find the field name that has the value 30
36. fieldName = getPropertyName(person, 30);
37. // Print the result, expected to be "age"
38. System.out.println(fieldName); // Outputs: age
39. }
40. // Nested static class Person with private fields
41. static class Person {
42. private String name;
43. private int age;
44. // Constructor to set the name and age of the person
45. public Person(String name, int age) {
46. this.name = name;
47. this.age = age;
48. }
49. }
50. }
Output:

name
age

2. setPropertyName()
For example, if the property name is firstName, the method name would be setFirstName() to write that
property. This method is called the mutator.

File Name: PropertySetter.java

1. import java.lang.reflect.Field;
2. public class PropertySetter {
3. // Method to set the property of an object based on the property name and the value provided
4. public static void setPropertyName(Object obj, String propertyName, Object value) {
5. try {
6. // Retrieve the Field object for the specified property from the class of the provided object
7. Field field = obj.getClass().getDeclaredField(propertyName);
8. // Make the field accessible, even if it is private or protected
9. field.setAccessible(true);
10. // Check if the value is compatible with the field type before setting it
11. if (isCompatible(value, field.getType())) {
12. // Set the field's value for the given object to the specified value
13. field.set(obj, value);
14. } else {
15. // Throw an exception if the value is not compatible with the field type
16. throw new IllegalArgumentException("Incompatible value type provided.");
17. }
18. } catch (NoSuchFieldException e) {
19. // Handle case where the field does not exist in the object
20. System.out.println("Field not found: " + propertyName);
21. } catch (IllegalAccessException e) {
22. // Handle case where the field is inaccessible or final
23. System.out.println("Failed to access or modify the field: " + propertyName);
24. } catch (IllegalArgumentException e) {
25. // Handle other illegal argument issues, such as type incompatibility
26. System.out.println(e.getMessage());
27. }
28. }
29. // Helper method to check if the provided value is compatible with the field's type
30. private static boolean isCompatible(Object value, Class<?> type) {
31. // Check if value is null (null can be assigned to any non-primitive field)
32. if (value == null) return true;
33. // Check if the value instance matches the field's type or its wrapper class
34. return type.isInstance(value) || (type.isPrimitive() && wrap(type).isInstance(value));
35. }
36. // Method to wrap primitive types in their corresponding wrapper classes
37. private static Class<?> wrap(Class<?> type) {
38. // Return the wrapper class for int types
39. if (type == int.class) return Integer.class;
40. // Return the wrapper class for double types
41. else if (type == double.class) return Double.class;
42. // Add more primitives if necessary
43. // Return the original type if it's not a primitive needing wrapping
44. return type;
45. }
46. \ public static void main(String[] args) {
47. // Create a new Person object
48. Person person = new Person("John Doe", 30);
49. // Display the person's name before modification
50. System.out.println("Before: " + person.getName());
51. // Change the person's name using the setPropertyName method
52. setPropertyName(person, "name", "Jane Doe");
53. // Display the person's name after modification
54. System.out.println("After: " + person.getName()); // Should output: Jane Doe
55. }
56. // Person class with private fields and a constructor
57. static class Person {
58. private String name;
59. private int age;
60. public Person(String name, int age) {
61. this.name = name;
62. this.age = age;
63. }
64. // Getter method for the name field
65. public String getName() {
66. return name;
67. }
68. }
69. }
Output:

Before: John Doe


After: Jane Doe

You might also like