1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

EJB 3.0 Stateful Session Bean Example

Discussion in 'Java' started by Sanskruti, May 7, 2007.

  1. Sanskruti

    Sanskruti New Member

    Joined:
    Jan 7, 2007
    Messages:
    108
    Likes Received:
    17
    Trophy Points:
    0
    Occupation:
    Software Consultant
    Location:
    Mumbai, India
    This article demonstrates how to write a simple Stateful Session Bean. It consists of three POJO's: Remote interface Cart.java, Bean class CartBean.java and EJB client CartClient.java.

    Prerequisites



    In order to complete the example application, you should be familiar with the following:
    * EJB 3.0​
    This demonstration requires that the following software components are installed and configured correctly:
    * Application Server such as J2EE, or other
    * Sun JDK version 1.5 or above​

    Remote Interface: Cart.java



    Code:
    package ejb_stateful;
    
    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();
    }
    

    Stateful Session Bean: CartBean.java


    Code:
    package ejb_stateful;
    
    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;
        }
    }
    

    EJB Client: CartClient.java


    Code:
    package ejb_stateful;
    
    import java.util.Collection;
    import java.util.Iterator;
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    
    public class CartClient {
        public static void main(String [] args) throws NamingException {
           
            try {
                final Context context = getInitialContext();
                Cart cart = (Cart)context.lookup("CartBean");
                
                System.out.println("Adding items to cart");
                
                cart.addItem("Pizza");
                cart.addItem("Pasta");
                cart.addItem("Noodles");
                cart.addItem("Bread");
                cart.addItem("Butter");
                
                
                System.out.println("Listing cart contents");
                Collection items = cart.getItems();
                for (Iterator i = items.iterator(); i.hasNext();) {
                String item = (String) i.next();
                System.out.println("  " + item);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    
        private static Context getInitialContext() throws NamingException {
            return new InitialContext();
        }
    }
    
    Compile all the programs, start the server and execute CartClient. You will see following Output:
    Code:
    Adding items to cart
    Listing cart contents
      Pizza
      Pasta
      Noodles
      Bread
      Butter
    
     
  2. nirav.ahm

    nirav.ahm New Member

    Joined:
    Aug 19, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi,
    I tried out this example using glassfish and netbeans but getting following error can you guide me?

    javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:284)
    at javax.naming.InitialContext.lookup(InitialContext.java:351)
    at ejb_stateful.CartClient.main(CartClient.java:14)


    Thanks
    Nirav Shah
     
  3. sabbir

    sabbir New Member

    Joined:
    Aug 25, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0

    just add app-serv-rt.jar(for GlassFish 2.0) in your client project it would be working fine

    Thanx
    Sabbirali Kadiwala
     
  4. BrentAston

    BrentAston New Member

    Joined:
    Oct 31, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    hawaii
    Hi :charming: :charming:



    :nonod: I tried out this example using glassfish and netbeans
     
  5. akash2008

    akash2008 New Member

    Joined:
    Jan 20, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    write a simple Stateful Session Bean. It consists of three POJO's: Remote interface Cart.java, Bean class CartBean.java and EJB client CartClient.java. Prerequisites In order to complete the example application, you should
     
  6. Venkat3

    Venkat3 New Member

    Joined:
    Jul 3, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    hello,

    i know that a stateful bean retains the state. but in this example, i don't see the strength of maintaining the "state" of the so called stateful bean.

    In this example, what is the so called "state"?
    Or can someone explain where in this example the difference with a stateless bean comes above???

    Thanks!
     
  7. abcde12345

    abcde12345 New Member

    Joined:
    Aug 2, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    What a dumb example. How about actually exercising the state of the session bean by making multiple calls. Calling it once demonstrates nothing.
     
  8. Sujata420

    Sujata420 New Member

    Joined:
    Feb 17, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    what are the jar files which i have import for this example? :confused:
     

Share This Page