{scrollbar}

This application will take you through the basics of Stateful Session Bean. This application will demonstrate how annotations like @Stateful, @Resource, @PostConstruct, @PreDestroy, @PrePassivate, @PostActivate, @Remove are used in an EJB3 application.

Basically a Stateful Session EJB is used whenever there is a requirement to maintain a session. The example is a user registration process wherein the registration process is a two step process. First page prompts to enter your personal credentials and second page prompts to enter your billing and credit card information. The session is maintained till the user has filled up both the jsp pages. Later the complete information is populated on to a database. The application has a Controller servlet which routes the call received from the jsp client to the Bean class, setter methods and jsp pages.

To run this tutorial, as a minimum you will be required to have installed the following prerequisite software.

Details on installing eclipse are provided in the Development environment section.
This tutorial is organized in the following sections:

Creating a EJB Project

  1. Right click Under Project Explorer and Select New->EJB Project.





  2. Name the project as StatefulBean. Select Next.





  3. Mark the fields as suggested in the screenshot and Select Next.





  4. Uncheck Generate Deployment Descriptor. This is beacuse we are using annotations is our applications and so deployment descriptors are redundant entity. Select Next.





  5. On next screen select all default values and Select finish.





    This creates a skeleton for the EJB project. Next steps are adding the bean class, bean interface and setter/getter methods.
  6. Right click on ejbModule in StatefulBean project and select New->class.





  7. Name the class as PersonalInfo and package as ejb.stateful. Select Finish.





  8. Add the following code to PersonalInfo.java.
    package ejb.stateful;
    
    public class PersonalInfo implements java.io.Serializable {
    private static final long serialVersionUID = 1L;
    
    private String FirstName;
    private String LastName;
    private String UserName;
    private String Password;
    private String Nationality;
    
    public void setFirstName(String FirstName)
    {
    	this.FirstName=FirstName;
    }
    
    public void setLastName(String LastName)
    {
    	this.LastName=LastName;
    }
    public void setUserName(String UserName)
    {
    	this.UserName=UserName;
    }
    public void setPassword(String Password)
    {
    	this.Password=Password;
    }
    public void setNationality(String Nationality)
    {
    	this.Nationality=Nationality;
    }
    public String getFirstName()
    {
    	return FirstName;
    }
    
    public String getLastName()
    {
    	return LastName;
    }
    public String getUserName()
    {
    	return UserName;
    }
    public String getPassword()
    {
    	return Password;
    }
    public String getNationality()
    {
    	return Nationality;
    }
    
    }
    



  9. Similarly create a class BillingInfo.java and add the following code.


    package ejb.stateful;
    
    public class BillingInfo implements java.io.Serializable
    {
    	
    	private static final long serialVersionUID = 1L;
    	private String houseNo;
    	private String street;
    	private String city;
    	private String pincode;
    	private String country;
    	private String bank;
    	private String cardno;
    	
    	public void setBank(String bank)
    	{
    		this.bank=bank;
    	}
    
    	public void setCardno(String cardno)
    	{
    		this.cardno=cardno;
    	}
    	public void setHouseNo(String houseNo)
    	{
    		this.houseNo=houseNo;
    	}
    
    	public void setStreet(String street)
    	{
    		this.street=street;
    	}
    	public void setCity(String city)
    	{
    		this.city=city;
    	}
    	public void setPincode(String pincode)
    	{
    		this.pincode=pincode;
    	}
    	public void setCountry(String country)
    	{
    		this.country=country;
    	}
    	public String getBank()
    	{
    		return bank;
    	}
    
    	public String getCardno()
    	{
    		return cardno;
    	}
    	public String getHouseNo()
    	{
    		return houseNo;
    	}
    
    	public String getStreet()
    	{
    		return street;
    	}
    	public String getCity()
    	{
    		return city;
    	}
    	public String getPincode()
    	{
    		return pincode;
    	}
    	public String getCountry()
    	{
    		return country;
    	}
    
    }
    
    PersonalInfo.java and BillingInfo.java are classes for setting and getting the user information.
  10. Now we will add the Business interface or bean interface. Right click on the package ejb.stateful and Select New->Interface.





  11. Name the interface as AccountCreator and Select Finish.





  12. Add the following code to AccountCreator interface.


    package ejb.stateful;
    import javax.ejb.Remote;
    @Remote
    public interface AccountCreator {
    void addPersonalInfo(PersonalInfo personalinfo);
    void addBillingInfo(BillingInfo billinginfo);
    void createAccount();
    }
    

    Once you enter this code you might see errors like @EJB can be resolved. Currently there are some limitations with the geronimo eclipse plugin which will resolved soon. We will soon suggest you how to get rid of those errors.




  13. Next step is to add the implementation to the interface. Right click on ejb.stateful interface and select New->class.





  14. Name the bean class as AccountCreatorBean and Select Finish.





  15. Add the following code to AccountCreatorBean.


    package ejb.stateful;
    
    import java.sql.Connection;
    
    import javax.annotation.PostConstruct;
    import javax.annotation.PreDestroy;
    import javax.annotation.Resource;
    import javax.ejb.PostActivate;
    import javax.ejb.PrePassivate;
    import javax.ejb.Remove;
    import javax.ejb.Stateful;
    import javax.sql.DataSource;
    
    
    @Stateful
    public class AccountCreatorBean implements AccountCreator{
    @Resource(name="jdbc/userds")
    DataSource datasource;
    Connection connection;
    PersonalInfo personalinfo=new PersonalInfo();
    BillingInfo billinginfo=new BillingInfo();
    	
    	public AccountCreatorBean() {
    	super();
    }	
    
    
    @PostConstruct
    @PostActivate
    public void openConnection()
    {
    	try{
    	connection=datasource.getConnection();
    	}
    	catch(Exception e)
    	{
    		e.printStackTrace();
    	}
    }
    
    @PreDestroy
    @PrePassivate
    public void closeConnection()
    {
    	connection=null;
    }
    
    public void addPersonalInfo(PersonalInfo personalinfo)
    {
    	this.personalinfo=personalinfo;
    }
    
    public void addBillingInfo(BillingInfo billinginfo)
    {
    	this.billinginfo=billinginfo;
    }
    
    @Remove
    public void createAccount()
    {
    	System.out.println("Your request has been successfully processed");
    }
    
    }
    
    Once you have added the code you will see lot of errors but this can be resolved easily and is shown in next step.
  16. The errors in the code is due to missing classes from our server runtime. This can be resolved as follows.
    Right click on StatefulBean project and select Properties.





  17. On the next screen select Java Build Path->Libraries->Add External Jars.





  18. Browse to <GERONIMO_HOME>/repository/org/apache/geronimo/specs/geronimo-ejb_3.0_spec/1.0.1 and select geronimo-ejb_3.0_spec-1.0.1.jar. Select Open.





  19. Similarly browse to <GERONIMO_HOME>/repository/org/apache/geronimo/specs/geronimo-annotation_1.0_spec/1.1.1 and add geronimo-annotation_1.0_spec-1.1.1.jar.





  20. Once done you can see both the jars enlisted. Select Ok.





  21. Let us walkthrough the EJB bean class code

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 userinfo.sql script. Select Run Sql.





    create table userinfo(firstname varchar(20),lastname varchar(20),
    username varchar(20), password varchar(20), pincode varchar(20),
    cardno varchar(20))
    



  8. To verify the table creation succeeded. Select Application as shown in the figure.





  9. Next screen suggests the table has been successfully created. To view the contents of the table select VIEW CONTENTS.





  10. The table is currently empty as shown in the figure.





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 a web based application client

  1. Right click under Project Explorer and Select New->Dynamic Web Project.





  2. Name the project as StatefulClient and Select Next.





  3. Keep the default settings as shown in the figure. Select Next.





  4. On the next screen keep default values. Select Next.





  5. Default values on this screen too. Select Finish.





  6. Right click on the StatefulClient project and Select New->Servlet.





  7. Name the package as ejb.stateful and Servlet as Controller.





  8. Keep the default values and Select Next.





  9. Keep the default values and Select Finish.


  10. Once the servlet is created it shows error. This is due to servlet api missing from the runtime. This can be easily resolved. Right click on StatefulClient project and Select properties.





  11. On the next screen select Java build path and select Libraries.





  12. Select Add External jars.





  13. Browse to your <GERONIMO_HOME>\repository\org\apache\geronimo\specs\geronimo-servlet_2.5_spec\1.1.2 and select geronimo-servlet_2.5_spec-1.1.2.jar and Select Open.





  14. Select Ok on the next screen this will remove all the errors.