Total members 11895 |It is currently Sun Dec 22, 2024 9:50 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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:
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

u need app-serv-rt.jar(for GlassFish 2.0)



_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : EJB 3.0 Stateful Session Bean Example
 EJB session bean     -  
 POJO JAVA BEAN     -  
 session in jsp     -  
 php login with session     -  
 PHP session variables     -  
 Session Valid     -  
 Delete session variable     -  
 Session handling using Struts     -  
 Login using session with php and mysql     -  
 tomcat session tracking     -  









Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com