Download EJB 2 - KSU Web Home

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
EJB 2.x Stateful Session Bean
Select File  New Project… and then choose the Enterprise category and Enterprise Application
project:
Click Next. Specify a project name and location:
In the ejb module, create three java class files.
Home interface (CartHome)
Remote interface (Cart)
Session bean class (CartBean)
Cart. Java
import java.util.*;
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
public interface Cart extends EJBObject {
public void addItem(String item) throws RemoteException;
public void removeItem(String item) throws RemoteException;
public Vector getCart() throws RemoteException;
}
CartBean.java
import java.util.*;
import javax.ejb.*;
import java.rmi.*;
public class CartBean implements SessionBean {
String name;
Vector cart;
SessionContext sessionContext;
//ejbCreate() is called back by EJB container after clients invoke
//create() method. Some initialization is done here. The main job
//here is to create a vector to hold all shopped items for this cart.
public void ejbCreate(String name)throws CreateException {
if (name == null) {
throw new CreateException("creation failed.");
}
else {
this.name = name;
}
cart = new Vector();
}
//Add an new item to the cart
public void addItem(String item) {
cart.add(item); }
//Remove an existing item from the cart
public void removeItem(String item) throws RemoteException {
boolean result = cart.remove(item);
if (result == false) {
throw new RemoteException("Can't find it");
}
}
//Return this cart
public Vector getCart() {
return cart;
}
public CartBean() {}
public void ejbRemove() {}
public void ejbActivate() {}
public void ejbPassivate() {}
public void setSessionContext(SessionContext sc){
sessionContext = sc;
}
}
CartHome.java
import java.rmi.RemoteException;
import javax.ejb.*;
public interface CartHome extends EJBHome {
public Cart create(String name) throws RemoteException, CreateException;
}
Right click on the Cart-ejb node and choose New-> Other->JaveEE->Standard Deployment
Descriptor
ejb-jar.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN'
'http://java.sun.com/dtd/ejb-jar_2_0.dtd'>
<ejb-jar>
<display-name>Cart-ejb</display-name>
<enterprise-beans>
<session>
<display-name>Cart-ejb</display-name>
<ejb-name>Cart-ejb</ejb-name>
<home>CartHome</home>
<remote>Cart</remote>
<ejb-class>CartBean</ejb-class>
<session-type>Stateful</session-type>
<transaction-type>Bean</transaction-type>
<security-identity>
<description></description>
<use-caller-identity></use-caller-identity>
</security-identity>
</session>
</enterprise-beans>
</ejb-jar>
Redo the above step and Right click on the Cart-ejb node and choose New->
Other->GlassFish->sun- Deployment Descriptor.
sun-ejb-jar.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD GlassFish Application Server 3.0
EJB 3.1//EN" "http://www.sun.com/software/appserver/dtds/sun-ejb-jar_3_1-0.dtd">
<sun-ejb-jar>
<enterprise-beans>
<ejb>
<ejb-name>Cart-ejb</ejb-name>
<jndi-name>CartBean</jndi-name>
</ejb>
</enterprise-beans>
</sun-ejb-jar>
In the Cart-app-client module, we should create CartClient.java file
CartClient.java
import java.util.Collection;
import java.util.Iterator;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
public class CartClient {
public static void main(String [] args) throws NamingException {
try {
Context initial = new InitialContext();
Object objref = initial.lookup("CartBean");
CartHome
home
(CartHome)PortableRemoteObject.narrow(objref,CartHome.class);
Cart cart = home.create("Duke");
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.getCart();
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();
=
}
}
Finally, after building and deploying the project, run it.
Related documents