Stateless beans are used in the case when the process or action can be completed in one go. In this case, object state will not be preserved in a series of linked invocation. Each invocation of bean will be unique in itself in a Stateless Session Bean. A stateless session bean in represented by @Stateless annotation.
Here is an example of Simple Stateless Session Bean:
As can be seen in the code, @Stateless defines a simple POJO as a Stateless Session bean.
The above gives a very high level idea of what is a stateless session bean and how is it implemented using EJB3 annotations. While working with examples we will try to make you understand stateless session beans in-depth.
Stateless Session Bean Sample
This sample application will take you through the basics of Stateless Session Bean. This application will demonstrate how annotations like @Stateless, @Resource, @PostConstruct, @PreDestroy are used in an EJB3 application.
The application walks through a authentication page, where the user has to authenticate to move to the resource page of Apache Geronimo. In case of a new user, the user has to go through the registration process. Later the user will be directed to login page again once the registration is done. In the login page the bean class will check for the username and password entered by the user against a database.
Basically a Stateless Session EJB is used whenever there is a single step process and maintaining a session is obsolete. In this sample the user registration form is a one step process and hence we have used stateless session bean for its implementation. The login page is a misnomer and should not be considered as an implementation for stateless session EJB. This is because once logged in you have to maintain the session and stateless session beans are not meant to maintain the session.
To run this tutorial, as a minimum you will be required to have installed the following prerequisite software.
- Sun JDK 5.0+ (J2SE 1.5)
- Eclipse 184.108.40.206 (Eclipse Classic package of Europa distribution), which is platform specific
- Web Tools Platform (WTP) 2.0.1
- Data Tools Platform (DTP) 1.5.1
- Eclipse Modeling Framework (EMF) 2.3.1
- Graphical Editing Framework (GEF) 3.3.1
Details on installing eclipse are provided in the Development environment section.
This tutorial is organized in the following sections:
Creating a Stateless Session EJB project
- Right click under the Project Explorer and Create an EJB project as shown.
- On the next screen give the project name as StatelessSessionEJB.
- Right click on ejbModule and create a new Interface RegisterBeanRemote.
- On the New Java Interface window give the package name as statelessejb and Interface name as RegisterBeanRemote.
- Populate the RegisterBeanRemote interface with the following code.
In the above code @Remote is a metadata annotation which marks the interface to be a Remote Interface. Metadata annotations are used to declare a class, interface or functions to have particular attributes. In this case the interface is marked to be a Remote Interface.
- Right click on StatelessSessionEJB project and create a new class RegisterBean as shown in the figure.
- Populate the class RegisterBean with the following data:
Lets try to understand this code.
- The very first line has an annotation @Stateless which declares the POJO to be a Stateless Session Bean. This annotation tells the container that the class is a Stateless Session Bean class. The container now automatically takes care of the various services like pooling, thread safety, transactions and so on.
- The second line suggests that the bean class implements the remote interface. Every EJB application has to have at least one interface. In this case we are having a remote interface.
- Next we have the @Resource(name="jdbc/userds") annotation which suggests jdbc/userds datasource being injected on to EJB. This is called dependency injection. The main idea behind dependency injection is that a component should call the resource through interfaces. This in turn will create a loosely coupled architecture between component and resource. Dependency injection is actually JNDI lookup in reverse order. In JNDI lookup the Bean looks up the resources itself and that is why it has to be hardcoded in the bean code itself whereas in Dependency Injection the Container reads the Bean and finds out what resources are required by the bean class. Later it injects the resources at runtime. How to create jdbc/userds datasource is discussed in the next section.
- Next interesting part is the @PostConstruct annotation. This annotation is used for lifecycle callbacks. A lifecycle callback method is used by container to inform the bean class of the state transitions in the bean lifecycle. @PostConstruct annotation is used once a bean instance is created. In our example we have our bean instance created and Dependency injected. Later @PostConstruct callback is invoked and a connection to the datasource is established.
- Next we have the register function which is used to populate the database USERINFO with user information. This function uses PreparedStatement to query the database.
- Next is the verify function which is used for the authentication of user credentials.
- @PreDestroy is again a lifecycle callback which suggests to release the resources before the bean is destroyed.
This completes the development of EJB project.
Creating a database using the administrative console
- Start the server and launch the Administrative Console using the URL http://localhost:8080/console.
- Enter default username and password.
- In the welcome page, Under Embedded DB, Select DB Manager.
- On the next page create a database userdbs and Select Create.
- Once done you can see the userdbs database listed in DB Viewer portlet under Databases. This confirms that the database has been successfully created.
- As shown in the figure under Use DB, select userdbs from the dropdown box.
- Run the query as shown in the figure. This query will create table USERINFO with the columns FIRSTNAME, LASTNAME, SEX, USERNAME, PASSWORD.
- To verify the table creation is successful. Under View Tables for userdbs Database, Select Application.
- The next window will show the table USERINFO associated with userdbs Database.
Creating a datasource using Administrative Console
- Start the server and Launch the Administrative Console using the URL http://localhost:8080/console.
- Enter default username and password.
- Once in the welcome page. In console navigation, Under Services, Select Database Pools.
- On the next screen, Create a new database pool using Geronimo database pool wizard.
- On the next screen give the name as suggested in the figure. This will initiate the process to create a Derby Embedded XA datasource.
- Select the Driver jar and give the database name as userdbs (Remember this is the database we created in the previous step). The rest of fields can be set to default.
- Select Deploy to deploy the connector plan.
- Once done you can see the Database Pool jdbc/userds listed in the available database pools.
Creating application client
- Create a new dynamic Web Project with the name ApplicationClient.
- Right click on WebContent and create the following
This form is the login page for our application. Once the user enters his/her credentials, these are passed to another jsp
passCredentials.jsp (checkout the action in the form tag) to verify the authenticity of user credentials. In case the user is new he has to go through the registration process. This statement <a href="http://localhost:8080/ApplicationClient/register.jsp">NewUser</a> is used to route the user to registration page.
<%!boolean i; %> is a declaration for a global variable. The other part is a scriptlet which does a JNDI lookup to the remote interface. Later the user credentials are passed to verify function. In case the credentials are correct user is routed to the resources page else he is redirected to login page to re-login.
This is the final page of the application which displays the various resources.
This page is the registration page. Once th user fills up the form and press Submit a valform() function is called which validates the field username and password. In case any of them is empty an alert is send which suggests empty fields in the form. If all is fine
passVariables.jsp is called.
In this page the fields are retrieved from
register.jsp and the register function in the bean class is called to populate the database.
The code <meta http-equiv="Refresh" content="5;URL=http://localhost:8080/ApplicationClient/login.jsp"> suggests to wait for 5 seconds and then move to login.jsp
Few more configurations
- In the EJB Project. Under META-INF, edit
openejb-jar.xml and add the following
openejb-jar.xml will look like this
- In the WEB Project. Under WEB-INF, Edit geronimo-web.xml and add the following
geronimo-web.xml will look like this
- Right click on the ApplicationClient Project and select properties. Select Java Build Path->Projects. Click Add and add Stateless Session EJB. This is required for the compilation of the Client code.
Deploy and Run
- Export the projects to your local disk as
- Start the server and launch the Administrative console using http://localhost:8080/console/. Enter default username and password.
- In the welcome page, under Applications. Select Deploy New.
- Browse and Select StatelessSessionEJB.jar. Once done Select Install. This will deploy the EJB application on to server.
- Browse and Select ApplicationClient.war. Once done select Install. This will deploy the Web application on to server.
- Under Applications, Select Web App WARs and Run ApplicationClient project as show in the figure.
- Once done a welcome page will be launched.
- If you are New User. Select New User as shown in the figure
- This will display a new registration form. If you leave the mandatory username or password filed empty. You will get the following error message.
- Fill the fields as shown in the figure and Select Submit.
- Once done a page will be displayed from where you will be automatically redirected to login page to relogin.
- Enter the username and password as chosen by you during registration and select Login.
- The next page is the Apache Geronimo Resource Center.
- In case you give wrong username and password combination you will be redirected to login page.