. Java Stateless Session EJB
Java Stateless Session EJB
A Stateless Session EJB
Note: This Example Uses J2EE 1.3
The Remote Interface - RockPaperScissors.java
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
public interface RockPaperScissors extends EJBObject
{
public String getRockPaperOrScissors()
throws RemoteException;
}
download source, use right-click and "Save Target As..." to save with a .java extension.
The Home Interface - RockPaperScissorsHome.java
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
public interface RockPaperScissorsHome extends EJBHome
{
RockPaperScissors create() throws RemoteException,
CreateException;
}
download source, use right-click and "Save Target As..." to save with a .java extension.
The Enterprise Bean Class - RockPaperScissorsBean.java
import java.util.Random;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
public class RockPaperScissorsBean implements SessionBean {
Random random;
//All methods in the Remote interface must be implemented here:
public String getRockPaperOrScissors() {
if (random == null) {random = new Random();}
int randomInt = random.nextInt(3);
if (randomInt == 0)
{return "Rock";}
else if (randomInt == 1)
{return "Paper";}
else
{return "Scissors";}
}
public void RockPaperScissorsBean() {}
//the following five methods must be in the Enterprise Bean Class:
public void ejbCreate() {}
//ejbCreate() must match Home Interface's create() method signature
public void ejbRemove() {}
public void ejbActivate() {}
public void ejbPassivate() {}
public void setSessionContext(SessionContext sessionContext) {}
}
download source, use right-click and "Save Target As..." to save with a .java extension.
ejb-jar.xml
A Client Application - RockPaperScissorsClient.java
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
public class RockPaperScissorsClient {
public static void main(String[] args) {
try {
Context initialContext = new InitialContext();
Object initialContextObject =
initialContext.lookup(
"java:comp/env/ejb/SimpleRockPaperScissors");
RockPaperScissorsHome rockPaperScissorsHome =
(RockPaperScissorsHome)PortableRemoteObject.narrow(
initialContextObject, RockPaperScissorsHome.class);
RockPaperScissors rockPaperScissors =
rockPaperScissorsHome.create();
String theBeansRPS =
rockPaperScissors.getRockPaperOrScissors();
String theApplicationsRPS = "Scissors";
//the application always picks "Scissors";
if (theBeansRPS.equals("Rock") &&
theApplicationsRPS.equals("Rock")) {
System.out.println("Tie!");
} else if (theBeansRPS.equals("Rock") &&
theApplicationsRPS.equals("Paper")) {
System.out.println("The Application wins!");
} else if (theBeansRPS.equals("Rock") &&
theApplicationsRPS.equals("Scissors")) {
System.out.println("The Bean wins!");
} else if (theBeansRPS.equals("Paper") &&
theApplicationsRPS.equals("Rock")) {
System.out.println("The Bean wins!");
} else if (theBeansRPS.equals("Paper") &&
theApplicationsRPS.equals("Paper")) {
System.out.println("Tie!");
} else if (theBeansRPS.equals("Paper") &&
theApplicationsRPS.equals("Scissors")) {
System.out.println("The Application wins!");
} else if (theBeansRPS.equals("Scissors") &&
theApplicationsRPS.equals("Rock")) {
System.out.println("The Application wins!");
} else if (theBeansRPS.equals("Scissors") &&
theApplicationsRPS.equals("Paper")) {
System.out.println("The Bean wins!");
} else if (theBeansRPS.equals("Scissors") &&
theApplicationsRPS.equals("Scissors")) {
System.out.println("Tie!");
} else {System.out.println("Someone didn't choose!");}
System.out.println("The Bean chose " + theBeansRPS +
", while the Application chose " +
theApplicationsRPS + ".");
rockPaperScissors.remove(); //clean up
} catch (Exception exception) {
System.err.println("Exception!");
exception.printStackTrace();
}
}
}
download source, use right-click and "Save Target As..." to save with a .java extension.
application-client.xml
A Client JSP - RockPaperScissorsJSP.jsp
download source, use right-click and "Save Target As..." to save with a .jsp extension.
References
| Sign In |
| to add your own comment |
| Comment by archive Rate this Comment |
In an effort to learn ejbs i scrapped your examples of the html page and created a tgz of them in which each bean type has a different sub-directory.
|