{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:
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; } } |
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; } } |
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. |
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"); } } |