The course is part of this learning path
In this course, we'll cover the basics of Enterprise JavaBeans (EJB) before moving on to explore the various components of the service. We'll round off the course with some example exam questions on business tier technologies.
Learning Objectives
- Learn the basics of EJB
- Learn about the various components of EJB:
- Stateless session and stateful session beans
- Singleton session beans
- Message-driven beans
Intended Audience
This course is intended for anyone who already has basic knowledge of Java and now wants to learn about Java Enterprise Edition.
Prerequisites
Basic knowledge of Java programming.
Hello dear friends. In this video, we'll examine session beans and stateful beans, let's begin. Stateful session bean is a business object that represents business logic like stateless session bean. Stateless beans provide business methods to their clients, but don't maintain a conversational state with them. Stateful session beans on the other hand, preserve conversational state. They are useful for tasks that have to be done in several steps, each of which relies on a state maintained in a previous step. Stateful session beans usually hold information about a specific client session and hold that information throughout the whole session. A stateful EJB Instance is coupled with only one client. One client can of course have many EJB instances in his session. Let's take the example of a shopping cart in an e-commerce website.
A customer logs on, chooses the first product, adds it to his shopping cart, chooses a second product and adds it to his cart. At the end, the customer checks out the products, pays for them and logs out. The shopping cart keeps the state of how many products the customer has chosen throughout the interaction. A session bean is not shared. It can have only one client, in the same way that an interactive session can only have 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.
There are five important annotations used in stateful session bean: stateful, PostConstruct, PreDestroy, PrePassivate, PostActivate. Now, let's look at the lifecycle of stateful bean. The client initiates the lifecycle by obtaining a reference to a stateful session bean. The container performs any dependency injection and then invokes the method annotated with PostConstruct, if any. The bean is now ready to have its business methods invoked by the client. When in the ready stage, the EJB container may decide to deactivate or passivate the bean by moving it from memory to secondary storage. The EJB container invokes the method annotated PrePassivate if any, immediately before passivating it.
If a client invokes a business method on the bean while it is in the passivate stage, the EJB container activates the bean, calls the method annotated PostActivated if any, and then moves it to the ready stage. At the end of the lifecycle, the client invokes a method annotated remove and the EJB container calls the method annotated PreDestroy, if any. The bean's instance is then ready for garbage collection. Your code controls the invocation of only one lifecycle method, the method annotated remove. All other methods are invoked by the EJB container. Now, if you want, let's see stateful bean with a sample code. First, you need to create bean component and bean client for creating a session bean application. This will be our remote interface.
As you can see, we have a remote annotation and an interface. Look, we have three methods. Now this code snip is the bean class and as you can see, we override methods and we have a stateful mapped name. After that, you will need a bean client. The stateful bean client maybe local, remote, or a Web service client. Here, this one is Web-based client and not using dependency injection. We have a form with three radio buttons and a button. As you can see with this form action, we will go to another page. Let's look at operation page codes. First condition is for withdrawal, second one is deposit, and last one is for balance. Of course, after this JSP, we need a doGet method. And the last code snip is our servlet with doGet method. Let's examine the doGet method. First, we have to create our initial context.
After that, we can define an instance from stateful with remote, and then we can set attributes to the request session. I hope you now have a better understanding of stateful session beans. In the following video, we will continue with the last session bean, Singleton session bean. See you in the next video.
OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.