Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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

  1. Right Click under the Project Explorer and Create a EJB project as shown.


    Image Added


  2. On the next screen give the project name as StatelessSessionEJB.


    Image Added


  3. Right Click on ejbModule and create a new Interface RegisterBeanRemote


    Image Added


  4. On the New Java Interface window give the package name as statelessejb and Interface name as RegisterBeanRemote


    Image Added


  5. Populate the RegisterBeanRemote interface with the following code.
    Code Block
    titleRegisterBeanRemote.java
    borderStylesolid
    
    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);
    }
    
    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.
  6. Right Click on StatelessSessionEJB project and create a new class RegisterBean as shown in the figure


    Image Added


  7. Populate the class RegisterBean with the following data
    Code Block
    titleRegisterBean
    Right Click under the Project Explorer and Create a EJB project as shown.
    Image Removed
    On the next screen give the project name as StatelessSessionEJB.
    Image Removed
    Right Click on ejbModule and create a new Interface RegisterBeanRemote
    Image Removed
    On the New Java Interface window give the package name as statelessejb and Interface name as RegisterBeanRemote
    Image Removed
    Populate the RegisterBeanRemote interface with the following code.
    Code Block
    titleRegisterBeanRemote.java
    borderStylesolid
    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);
    }
    
    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
    Image Removed
    Populate the class RegisterBean with the following data
    Code Block
    titleRegisterBean.java
    borderStylesolid
    
    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();
    		}
    
    	}
    }
    
    Lets try to understand this code
  8. 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.
  9. 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.
  10. 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.
  11. 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.
  12. 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.
  13. Next is the verify function which is used for the authentication of user credentials.
  14. @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
titleWarning

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
<openejb-jar xmlns="http://www.openejb.org/xml/ns/openejb-jar-2.2" xmlns:nam="http://geronimo.apache.org/xml/ns/naming-1.2" xmlns:pkgen="http://www.openejb.org/xml/ns/pkgen-2.0" xmlns:sec="http://geronimo.apache.org/xml/ns/security-1.2" xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.2">

Image Modified


This completes the development of EJB project.

Creating a database using Administrative Console

...

  1. Export the projects to your local disk as StatelessSessionEJB.jar and ApplicationClient.war.
  2. Start the server and launch the Administrative console using http://localhost:8080/console/. Enter default username and password.
  3. In the welcome page, under Applications. Select Deploy New.





  4. Browse and Select StatelessSessionEJB.jar. Once done Select Install. This will deploy the EJB application on to server.
  5. Browse and Select ApplicationClient.war. Once done select Install. This will deploy the Web application on to server.
  6. Under Applications, Select Web App WARs and Run ApplicationClient project as show in the figure.





  7. Once done a welcome page will be launched.





  8. If you are New User. Select New User as shown in the figure





  9. This will display a new registration form. If you leave the mandatory username or password filed empty. You will get the following error message.





  10. Fill the fields as shown in the figure and Select Submit.





  11. Once done a page will be displayed from where you will be automatically redirected to login page to relogin.





  12. Enter the username and password as chosen by you during registration and select Login.





  13. The next page is the Apache Geronimo Resource Center.





  14. In case you give wrong username and password combination you will be redirected to login page.





Exporting the application as a plugin