You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 6 Next »

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 home page of Apache Geronimo. In case 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.

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:

Creating a Stateless Session EJB project

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





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





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





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





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





  7. Populate the class RegisterBean with the following data
    RegisterBean.java
    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.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";
    		System.out.println(select);
    		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;
    			else
    			    ret=false;
    			}
    			else
    				ret=false;
    		}
    		}
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    		return ret;	
    	}
    	@PreDestroy
    	public void destroy(){
    		try
    		{
    		dbconnect.close();
    		dbconnect=null;
    		}
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    
    	}
    }
    
    
    Next we will try to understand the code

This completes the development of EJB project.

Creating a database using Administrative Console

  1. Start the server and Launch the Administrative Console using the URL http://localhost:8080/console.
  2. Enter default username and password.
  3. In the welcome page, Under Embedded DB, Select DB Manager.





  4. On the next page create a database userdbs and Select create.





  5. Once done you can see the userdbs database listed in DB Viewer portlet under Databases. This confirms that the database has been successfully created.





  6. As shown in the figure under Use DB, select userdbs from the dropdown box.





  7. Run the query as shown in the figure. This query will create table USERINFO with the columns FIRSTNAME, LASTNAME, SEX, USERNAME, PASSWORD.





    CreateTable.sql
    CREATE TABLE USERINFO
    (
    FIRSTNAME char(20),
    LASTNAME  char(20),
    SEX       char(7),
    USERNAME  char(20),
    PASSWORD  char(20)
    )
    
  8. To verify the table creation is successful. Under View Tables for userdbs Database, Select Application.





  9. The next window will show the table USERINFO associated with userdbs Database.





Creating a datasource using Administrative Console

  1. Start the server and Launch the Administrative Console using the URL http://localhost:8080/console.
  2. Enter default username and password.
  3. Once in the welcome page. In console navigation, Under Services, Select Database Pools.





  4. On the next screen, Create a new database pool using Geronimo database pool wizard.





  5. On the next screen give the name as suggested in the figure. This will initiate the process to create a Derby Embedded XA datasource.





  6. Select the Driver jar and give the database name as userdbs(Remember this is the database we created in the previous step). Rest all fields can be set to default.





  7. Select Deploy to deploy the connector plan.





  8. Once done you can see the Database Pool jdbc/userds listed in the available database pools.





Creating application client

Few more configurations

  1. In the EJB Project. Under META-INF, Edit openejb-jar.xml and add the following
    datasource dependency
    <sys:dependencies>
                <sys:dependency>
                    <sys:groupId>console.dbpool</sys:groupId>
                    <sys:artifactId>jdbc%2Fuserds</sys:artifactId>
                </sys:dependency>        
    </sys:dependencies>
    

    Where did the above dependencies come from??

    To make the datasource visible to EJB we need to add a dependency to the EJB deployment plan that is openejb-jar.xml. The above element can be obtained automatically from Geronimo Database Pool wizard. Select usage against the database pool jdbc/userds

  • No labels