Versions Compared

Key

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

...

  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.
    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





  7. 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.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");
    			String str1=rs.getString("PASSWORD");
    			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();
    		}
    
    	}
    }
    
  8. Lets try to understand this code
    1. 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.
    2. The second line suggests that the Bean Class implements the remote interface. Every EJB application has to have atleast one interface. In this case we are having a remote interface.
    3. 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. Remember we created the How to create jdbc/userds datasource is discussed in the previous next section.
    4. 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 when once a bean instance is created. In our case example we have our bean instance created and Dependency injected. Once this is done the Later @PostConstruct callback is invoked and a connection to the datasource is established.
    5. 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.
    6. Next function is the verify function which is used for the authentication of user credentials.
    7. @PreDestroy is again a lifecycle callback which suggests to release the resources before the bean is destroyed.

      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">




      This completes the development of EJB project.

...