Objective: In this article I will introduce about EJB(Enterprise Java Bean) , its components and I will illustrate a small application of EJB.

Watching: What is Ejb

1. What is EJB?

– EJB is a component located on the server-side of a web application or can be understood as a component in the Java EE architecture.

– The main task is to handle business logic and data access. EJB can be understood as the Model in the MVC2 model I mentioned in previous posts.

– EJB is a platform that allows building software that is portable, highly reusable, and security is also an important feature of EJB.

– Another important feature is that EJB is a component towards large-scale applications, suitable for distributed models.

– Place of EJBs is in an EJB container. This container will store, schedule, and manage EJBs.

2. Types of EJB

EJBs are divided into 3 main categories.

Entity beans: Similar to entity objects, contain task information and its operation methods.

– Session beans. Manage client and server tasks. The client will interact with the server by invoking session bean methods through a certain network environment that can be HTTP (Hyper text transfer protocol). Session Bean calls the corresponding entity bean to perform the task that the client does. request. Session beans can be divided into two main types.

Stateless: Client state will not be saved for future transactions. The different transactions will be completely independent of each other.

+ Statefull: The transaction state of the client will be stored for the next time.

Message-driver bean: Responsible for handling messages between client and server.

READ MORE  Meaning of Distribution

3. Demo 1 small app about EJB.

In this example, I will guide you to create a small application about EJB, the only task of the application is to update an employee’s information to the database.

Creating Project named EmpEnterpriseApplication.

First. create Project . Chonnj Java EE, Then choose Enterprise Application.

Next. Name the project, select the server as Glassfish Server. and finish.

Below is the structure of the Project, I created 2 more packages named entity.example, this package contains entities that will be mapped with tables in the database, each table corresponds to each entity.

The second package I named session.example, this package contains sessions to invoke entity beans when requesting from the client.

The image below is the interface when the application runs.

Here is the code in the index. jsp.

See also: What is Market Research, Concepts and Methods of Implementation

The next job we will create a database, I will use the database right on netbean.

Click Service -> click on Database -> create new database.

Fill in information such as database name, user name, password and click OK.

After creating . You notice that the folder named Java DB has a database named empDBB. right click and select connect.

The blue text I blacked out, right-click and select connect, after connecting we will create a table named Employee with the following columns.

All columns are of type varchar with a size of (30) and the ID column is the primary key.

ok. Now we will return to the entity.example.com package. I will create a bean by binding to the Employee table of the database we just created.

READ MORE  The Right Definition Of Startup

Create a bean and select Entity CLAs from Database. Then in the Data Source section, we choose new data source.

Name the JNDI-component to Map intity bean and Databse.

Select the database we just created.

Then add bind which type of table you want to bind to. click add all and finish.

So we are done with the intity bean part.

Next will be the Session Bean.

Next select Local and finish.

The file will be generated as follows.

Here is the code in the class named EmployeeFacade

Next we will have to create a Servlet to receive requests from the server.

How to create servlett I will not talk about here. You create a servlet named empSelvlet.

Final. Clean and Build -> Deploy -> Run.

Add information, and see what will be in the database. Good luck.

See also: What is Blossom

In this article, I do not go into EJB, I only introduce the overview and role of EJB in Java EE architecture. I will cover EJB in detail in the following series of tutorials.