Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Wiki Markup
{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.

...

  • 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 EJB Project

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


    Image Added


  2. Name the project as StatefulBean. Select Next.


    Image Added


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


    Image Added


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


    Image Added


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


    Image Added


    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.


    Image Added


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


    Image Added


  8. Add the following code to PersonalInfo.java.
    Code Block
    titlePersonalInfo.java
    borderStylesolid
    
    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;
    }
    
    }
    

...




  1. Similarly create a class BillingInfo.java

...

  1. and

...

  1. add

...

  1. the

...

  1. following

...

  1. code.

...




  1. Code Block
    titleBillingInfo.java

...

  1. borderStyle

...

  1. solid

...

  1. 
    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;
    	}
    
    }
    

...

  1. PersonalInfo.java

...

  1. and

...

  1. BillingInfo.java

...

  1. are

...

  1. classes

...

  1. for

...

  1. setting

...

  1. and

...

  1. getting

...

  1. the

...

  1. user

...

  1. information.

...

  1. Now

...

  1. we

...

  1. will

...

  1. add

...

  1. the

...

  1. Business

...

  1. interface

...

  1. or

...

  1. bean

...

  1. interface.

...

  1. Right

...

  1. click

...

  1. on

...

  1. the

...

  1. package

...

  1. ejb.stateful

...

  1. and

...

  1. Select

...

  1. New->Interface.

...




  1. Image Added


  2. Name the interface as AccountCreator and Select Finish.


    Image Added


  3. Add the following code to AccountCreator interface.


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

...

  1. Info
    titleInformation

    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.




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


    Image Added


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


    Image Added


  4. Add the following code to AccountCreatorBean.


    Code Block
    titleAccountCreatorBean.java
    borderStylesolid
    
    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");
    }
    
    }
    

...

  1. Once

...

  1. you

...

  1. have

...

  1. added

...

  1. the

...

  1. code

...

  1. you

...

  1. will

...

  1. see

...

  1. lot

...

  1. of

...

  1. errors

...

  1. but

...

  1. this

...

  1. can

...

  1. be

...

  1. resolved

...

  1. easily

...

  1. and

...

  1. is

...

  1. shown

...

  1. in

...

  1. next

...

  1. step.

...

  1. The

...

  1. errors

...

  1. in

...

  1. the

...

  1. code

...

  1. is

...

  1. due

...

  1. to

...

  1. missing

...

  1. classes

...

  1. from

...

  1. our

...

  1. server

...

  1. runtime.

...

  1. This

...

  1. can

...

  1. be

...

  1. resolved

...

  1. as

...

  1. follows.

...


  1. Right

...

  1. click

...

  1. on

...

  1. StatefulBean

...

  1. project

...

  1. and

...

  1. select

...

  1. Properties.


    Image Added


  2. On the next screen select Java Build Path->Libraries->Add

...

  1. External

...

  1. Jars.


    Image Added


  2. Browse to <GERONIMO_HOME>/repository/org/apache/geronimo/specs/geronimo-ejb_3.0_spec/1.0.1

...

  1. and

...

  1. select

...

  1. geronimo-ejb_3.0_spec-1.0.1.jar

...

  1. .

...

  1. Select

...

  1. Open.


    Image Added


  2. Similarly browse to <GERONIMO_HOME>/repository/org/apache/geronimo/specs/geronimo-annotation_1.0_spec/1.1.1

...

  1. and

...

  1. add

...

  1. geronimo-annotation_1.0_spec-1.1.1.jar

...

  1. .


    Image Added


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


    Image Added


  3. Let us walkthrough the EJB bean class code
    • @Stateful public class AccountCreatorBean implements AccountCreator- @ Stateful annotation declares the Bean class as Stateful class.
    • @Resource(name="jdbc/userds")

...

    • DataSource

...

    • datasource;-

...

    • This

...

    • is

...

    • a

...

    • resource

...

    • injection

...

    • into

...

    • the

...

    • bean

...

    • class

...

    • wherin

...

    • a

...

    • datasource

...

    • is

...

    • injected

...

    • using

...

    • the

...

    • @Resource

...

    • annotation.

...

    • We

...

    • will

...

    • shortly

...

    • see

...

    • how

...

    • to

...

    • create

...

    • a

...

    • datasource

...

    • in

...

    • geronimo.

...

    • public

...

    • AccountCreatorBean(

...

    • } -

...

    • This

...

    • is

...

    • a

...

    • constructor

...

    • for

...

    • the

...

    • bean

...

    • class

...

    • and

...

    • it

...

    • will

...

    • be

...

    • used

...

    • to

...

    • create

...

    • a

...

    • bean

...

    • instance

...

    • whenever

...

    • a

...

    • request

...

    • is

...

    • received

...

    • from

...

    • new

...

    • client

...

    • connection.

...

    • @PostConstruct

...

    • @PostActivate

...

    • public

...

    • void

...

    • openConnection()-

...

    • @PostConstruct

...

    • and

...

    • @PostActivate

...

    • are

...

    • annotations

...

    • which

...

    • are

...

    • basically

...

    • called

...

    • lifecycle

...

    • callback

...

    • annotation.

...

    • The

...

    • lifecycle

...

    • for

...

    • these

...

    • annotation

...

    • is

...

    • as

...

    • follows

...

      • New

...

      • bean

...

      • instance

...

      • is

...

      • created

...

      • using

...

      • the

...

      • default

...

      • constructor.

...

      • Resources

...

      • are

...

      • injected

...

      • Now

...

      • the

...

      • PostConstruct

...

      • method

...

      • is

...

      • called

...

      • which

...

      • in

...

      • our

...

      • case

...

      • is

...

      • to

...

      • open

...

      • a

...

      • database

...

      • connection.
      • PostActivate is called on the bean instances which have been passivated and required to be reactivated. It goes on the same cycle as being followed by PostConstruct.
    • @PreDestroy @PrePassivate public void closeConnection()- Again @PreDestroy and @PrePassivate are Lifecycle callback annotation. The lifecycle of these annotation is as follows
      • Bean instances in the pool are used and business methods are invoked.
      • Once the client is idle for a period of time container passivates the bean instance. The closeConnection function is called just before container passivates the bean.
      • If the client does not invoke a passivated bean for a period of time it is destroyed.