View RSS Feed

EJB

Stateful Session Bean

Rate this Entry
by
EJB
, 01-02-2012 at 05:52 PM (935 Views)
A stateful session bean is an enterprise bean (EJB component) that serves to be a client’s server side extension. It is created by one client and hence it works for just that client till the drop takes place in client connection or removal of the bean takes place explicitly. The stateful Bean (EJB Component) implements the interface javax.ejb.SessionBean and deploys to the “stateful” which is the declarative attribute. They are called stateful as conversational state is being maintained by them with client.

Java Code: This is the example code for stateful session bean
import java.util.Collection;
import javax.ejb.Remote;
@Remote
public interface Cart {
    public void addItem(String item);
    public void removeItem(String item);
    public Collection getItems();
}

import java.util.ArrayList;
import java.util.Collection;
import javax.annotation.PostConstruct;
import javax.ejb.Stateful;
@Stateful
public class CartBean implements Cart {
    private ArrayList items;
    @PostConstruct
    public void initialize() {
        items = new ArrayList();
    } 
    public void addItem(String item) {
        items.add(item);
    }
    public void removeItem(String item) {
        items.remove(item);
    }
    public Collection getItems() {
        return items;
    }
}

Submit "Stateful Session Bean" to Facebook Submit "Stateful Session Bean" to Digg Submit "Stateful Session Bean" to del.icio.us Submit "Stateful Session Bean" to StumbleUpon Submit "Stateful Session Bean" to Google

Updated 01-14-2012 at 06:12 PM by EJB

Categories
Session Bean

Comments