# Getting Started: Enterprise JavaBeans

## The Problem of Organizing Business Logic

When you model a real-world problem in a software application, you are faced with modeling the behavior of multiple business objects including the rules that govern their interactions. To model the business objects, you need some way to represent their properties. To model the rules, you need to be able to break the business logic into smaller subtasks that comprise a complex interaction, and you need to have some way to encapsulate these subtasks such that they can be reused to model other interactions. For instance, if your software application is an online solution for a customer buying books or CDs, you need a way to represent customers, books, CDs, credit cards, and so forth. In addition, buying a CD and buying a book are two tasks that have common subtasks, such as the use of a credit card to pay for the purchased item. You want to be able to separate this subtask and re-use it in both cases.

## The Enterprise JavaBean Solution

The Enterprise JavaBeans (EJB) technology solves this problem by providing specialized server-side components to handle data and to encapsulate business logic. Entity beans are the EJBs that specialize in representing business objects, such as customers, books, CDs, and credit cards. An entity bean interacts with a database to ensure that business object properties are stored in a persistent manner. Session beans are the EJBs that specialize in encapsulating business logic and governing the interactions between business objects. Furthermore, these EJBs operate in an EJB container on the server. The EJB container takes care of managing other processes that are crucial for an application to work successfully but are not an integral part of the application design. For example, the EJB container takes care of transaction management such that when an error occurs during the online purchase of a book, the customer does not end up being charged for a book that he will not receive. In other words, if a transaction fails, the EJB container makes sure that any changes made during that transaction (such as charging a credit card) are rolled back.

## What This Tutorial Teaches

In this tutorial, you will learn the basics of using EJBs to encapsulate business logic and represent business objects. You will create two EJBs to model the server-side logic of a simplified login procedure. The procedure consists of a user submitting a name, and receiving a greeting that is based on the number of times the user has submitted his/her name previously. To model this procedure you will create (a) an entity bean that creates records in a database regarding the number of visits by a user, and (b) a session bean that will receive the user input, query the entity bean to find out the status of this user, and return a status-specific greeting.

Related Topics

Getting Started with EJB Project

Click the arrow to navigate to the next step.