Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to work with Enterprise JavaBeans (EJB)

In this article we will see an overview about EJB(Enterprise JavaBeans), one of the most used frameworks in Java.

Introduction to EJB:

Enterprise JavaBeans (EJB) is the server side technology available in Java Enterprise Edition (EE). The J2EE and EJB both are able to provide the support in order to create the web based applications that are in the form of an enterprise level. An enterprise bean is the server side component which is responsible in order to implement the business logic for the enterprise level application. This enterprise bean is present in the container of EJB which is basically a runtime environment present in the server side of enterprise edition of java and this container provides different services in order to support the enterprise beans in the respective manner.

What is Enterprise Java Beans?

Enterprise JavaBeans consists of the business logic and the container available in EJB has the ability to create and in order to maintain the instances of EJB. EJB defines a set of interfaces and components and by using EJB the scalable, reliable and secure applications can be built in an effective manner. There are distributed objects on which the components of EJB are based and this distributed object can be called by a remote system and it enables the user to distribute the application over the network. The distributed objects include a stub and a skeleton. The client calls a stub which basically is responsible to handle the communication over network and stub calls over network with the use of sockets. This call leads to skeleton from network and the skeleton then takes the responsibility to handle the network communication from distributed object and skeleton receives the calls on the respective socket. The classes of EJB are present in the javax.ejb.* package and in order to access the functionalities of these classes this package is to be imported in the application. The two interfaces known as Home and Remote are used in EJB.

EJB is the powerful facility used in order to express the business logic of an application. This technology is used in order to access the database with the help of concepts of java beans. EJB is the standard server side component based architecture in order to manage, develop and deploy the applications in the useful and productive environment. Container is basically a server that provides some service and maintains the control using an interface and there is an EJB container which is required for the components of EJB. The distributed component based applications can be easily built in java and the process is also simple. EJB consists of different types including session beans, message driven beans and entity beans. The session beans are divided into three types which include stateless session beans, stateful session beans and singleton session beans. The entity beans are divided into two types which include bean managed persistence and container managed persistence. Session bean is known as the transient or non-persistent object and entity bean is known as the persistent object. The session bean is used in order to model the business process and it represents one client that is present in the Application Server (AS). The session bean is responsible to perform work for the client and the tasks related to business are executed in the server and client is saved from this difficulty. When the client exits the session bean also exits because this session bean is not shared and is non-persistent. In the stateless session bean, the state cannot be maintained for the client across the methods calls. In the stateful session bean, the state can be maintained for the client across the method calls. The singleton session bean is required to be instantiated only once and it exists for the entire life of an application. The entity bean is used in order to model the business data and it represents the business object that is present in a persistent storage mechanism and each entity bean consists of a table in the database and an instance of bean matches to the particular row in that respective table. The real world business objects can be the customers and the products respectively. The message driven bean is an entity bean that allows the applications of java enterprise edition in order to process the message and acts as the message listener of Java Messaging Service (JMS) and receives the message but not the events therefore it is known as the message driven bean respectively. JMS is used in order to send the message to particular client using beans. In the entity beans with bean managed persistence there is a need to write code containing the statements of SQL in order to access the data from the database and the implementation in this type of bean is dependent on database. In the entity beans with container managed persistence, the persistent columns of entity are mapped to the columns of table in a database and in this type of bean the container has the ability in order to generate the code for data access and this CMP is independent of database unlike BMP.

Importance to EJB:

Some of the characteristics of an enterprise application include Remote Method Invocation (RMI) which connects the client and server, backend integration which is the integration of database with legacy systems, clustering, logging and auditing. EJB container is responsible in order to pool the resources, register an object, to provide a remote interface to the object, creating and destroying the instances of objects and managing the state of an object respectively and others. An enterprise bean runs in an EJB container that is runtime environment and present inside the Application Server (AS). The container of EJB is responsible in order to ensure that the code present in client side contains enough access rights to an EJB and the aspects of security are applied in a declarative manner through the use of annotations to the bean of EJB respectively. The EJB container is also responsible for the transaction management that is a system level service.

Screenshots:

The work of stateless session beans is shown with the help of running applications and all the necessary required details.

Running applications and all the necessary required details

Figure 1: Running applications and all the necessary required details

The Figure-01 shows the total projects required including the java class library in order to complete the work of in a successful manner. The projects include the following; SessionBeanwork is the application client module, EJB_SessionBeans_Work-ejb is the EJB module, JavaClassLibrary_for_EJB_work and EJB_SessionBeans_Work is an enterprise application project as shown in the left side that are inside the Projects tab of Netbeans.

Work being done in EJB Module

Figure 2: Work being done in EJB Module

The Figure 2 shows the work being done in the EJB module and applying the required functionality in order to execute the stateless session bean in a successful manner. The generated codes are also shown under the heading of implementation and code for all the respective files.

Adding the Business Method

Figure 3: Adding the Business Method

The Figure 3 shows the windows to add the business method which will be called.

Work being done in SessionBeanwork Project of client side

Figure 4: Work being done in SessionBeanwork Project of client side

The Figure 4 shows the work being done in the client side project and main class of SessionBeanwork. If the work of all related projects and their configurations are included correctly then this code will be generated in a successful manner.

Adding functionality in Main class of SessionBeanwork

Figure 5: Adding functionality in Main class of SessionBeanwork

In the Figure 5, the highlighted function will be called when the projects are included in the java class library and in the SessionBeanwork and the configurations work successfully completed.

The message present in the EJB Module shown

Figure 6: The message present in the EJB Module shown

The required message to be displayed

Figure 7: The required message to be displayed

In the Figure 6 and Figure 7, the highlighted message as in the Figure-06 will be shown when the projects are included in the library and SessionBeanwork and configurations done in a successful manner.

Project Name: JavaClassLibrary_for_EJB_work

In the project of JavaClassLibrary_for _EJB_work, there comes a remote interface.

Listing 1: Main_cworkRemote.java

package com.ejb.session.bean;
import javax.ejb.Remote;
@Remote
public interface Main_cworkRemote {
  String businessMethod(String thename);    
}

This is the remote interface present in the java class library project.

Project Name: SessionBeanwork

In the project of SessionBeanwork, the java class library and the project of EJB module is required to be added in order to get the desired results.

Listing 2: Main.java

package com.ejb.session.bean;
import javax.ejb.EJB;
import com.ejb.session.bean.Main_cworkRemote;
import javax.swing.JOptionPane;
public class Main {
    @EJB
    private static Main_cworkRemote main_cwork;
    public static void main(String[] args) {
        // TODO code application logic here        
        JOptionPane.showMessageDialog(null, main_cwork.businessMethod("Work has been successfully done"));
//        System.out.println(main_cwork.businessMethod(""));
    }
}

This class calls the message available in the project of EJB module which is present in the enterprise application respectively.

Project Name: EJB_SessionBeans_Work-ejb

This project is present inside the enterprise application whose name as in the first screenshot shown is EJB_SessionBeans_Work.

Listing 3: Main_cwork.java

package com.ejb.session.bean;
import javax.ejb.Stateless;
@Stateless
public class Main_cwork implements Main_cworkRemote {
    private String the_name;
    public String set_the_name(){
        the_name="Hello Where is the Key of My Car!";
        return the_name;
    }
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    public String businessMethod(String thename) {
        the_name=set_the_name();
        thename="IT is Present in the Computer Table";
        return the_name+thename;
    }
}

It contains a functions named as businessMethod and this is called by the client side as discussed above in detail with the help of screenshots and codes respectively.

Conclusion

As we have already described different sections of enterprise java beans in our discussion. Enterprise java beans are the widely used business tier components used in java and j2ee applications. The business logic is mainly handled in this section and then delegates it to the persistence layer. So a good understanding of enterprise java beans is essential for java and j2ee application developers.



My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login