...
This 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 need 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 3.3.1.1 (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:
Table of Contents |
---|
Creating a Stateless Session EJB project
plugin export and import using Apache Geronimo. We will enlist the application development and deployment first. Later we will use Apache Geronimo to Export the application and dependencies as plugin and import the same.
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 3.3.1.1 (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:
Table of Contents |
---|
Creating a Stateless Session EJB project
- Right Click under the Project Explorer and Create a 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.Code Block title RegisterBeanRemote.java borderStyle solid package statelessejb; import javax.ejb.Remote; @Remote public interface RegisterBeanRemote { public void register(String FirstName, String LastName, String Sex, String UserName, String Password); public boolean verify(String username, String password); }
- Right Click on StatelessSessionEJB project and create a new class RegisterBean as shown in the figure
- Populate the class RegisterBean with the following data
Right Click under the Project Explorer and Create a EJB project as shown.Code Block title RegisterBean
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 figureCode Block title RegisterBeanRemote.java borderStyle solid package statelessejb; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import javax.ejbannotation.RemotePostConstruct; @Remote public interface RegisterBeanRemote { public void register(String FirstName, String LastName, String Sex, String UserName, String Password); public boolean verify(String username, String password); }
Populate the class RegisterBean with the following data
Lets try to understand this codeCode Block title RegisterBean.java borderStyle solid package statelessejb; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.annotation.Resource; import javax.ejb.Stateless; import javax.sql.DataSource; @Stateless public class RegisterBean implements RegisterBeanRemote{ @Resource(name="jdbc/userds") private DataSource dbsource; private Connection dbconnect; @PostConstruct public void initialize() { try{ dbconnect= dbsource.getConnection(); } catch(Exception e) { e.printStackTrace(); } } public void register(String FirstName, String LastName, String Sex, String UserName, String Password) { try { String insert="INSERT INTO USERINFO (" + "FIRSTNAME," + "LASTNAME," + "SEX," + "USERNAME," + "PASSWORD)" + " VALUES(?,?,?,?,?)"; PreparedStatement ps=dbconnect.prepareStatement(insert); ps.import javax.annotation.PreDestroy; import javax.annotation.Resource; import javax.ejb.Stateless; import javax.sql.DataSource; @Stateless public class RegisterBean implements RegisterBeanRemote{ @Resource(name="jdbc/userds") private DataSource dbsource; private Connection dbconnect; @PostConstruct public void initialize() { try{ dbconnect= dbsource.getConnection(); } catch(Exception e) { e.printStackTrace(); } } public void register(String FirstName, String LastName, String Sex, String UserName, String Password) { try { String insert="INSERT INTO USERINFO (" + "FIRSTNAME," + "LASTNAME," + "SEX," + "USERNAME," + "PASSWORD)" + " VALUES(?,?,?,?,?)"; PreparedStatement ps=dbconnect.prepareStatement(insert); ps.setString(1,FirstName); ps.setString(2,LastName); ps.setString(3,Sex); ps.setString(4,UserName); ps.setString(5,Password); int rs =ps.executeUpdate(); } catch(Exception e) { e.printStackTrace(); } } public boolean verify(String username, String password) { boolean ret=false; String select="SELECT * FROM USERINFO"; try{ PreparedStatement ps=dbconnect.prepareStatement(select); ResultSet rs= ps.executeQuery(); while(rs.next()) { String str=(rs.getString("USERNAME")).trim(); String str1=(rs.getString("PASSWORD")).trim(); if (str.compareTo(username)==0) { if(str1.compareTo(password)==0) { ret=true; break; } else ret=false; } else ret=false; } } catch(Exception e) { e.printStackTrace(); } System.out.println(ret); return ret; } @PreDestroy public void destroy(){ try { dbconnect.close(); dbconnect=null; } catch(Exception e) { e.printStackTrace(); } } }
- 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 etc.
- 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.
dbconnect.close();
dbconnect=null;
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
|
Warning | ||
---|---|---|
| ||
Due to some limitations in Geronimo Eclipse Plugin the generated deployment plan(openejb-jar.xml) does not have the correct namespace. Replace the existing namespace as shown in the figure with the following |
This completes the development of EJB project.
Creating a database using Administrative Console
...
- Export the projects to your local disk as StatelessSessionEJB.jar and ApplicationClient.war.
- 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.