Java Stateful Session EJB
The Remote Interface - SayMyName.java
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
public interface SayMyName extends EJBObject
{
public String getMyName() throws RemoteException;
public int getCount() throws RemoteException;
public void setMyName(String nameIn) throws RemoteException;
}
The Home Interface - SayMyNameHome.java
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
public interface SayMyNameHome extends EJBHome {
SayMyName create() throws RemoteException, CreateException;
}
The Enterprise Bean Class - SayMyNameBean.java
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
public class SayMyNameBean implements SessionBean {
String myName;
int countNameCalled;
//All methods in the Remote interface must be implemented here:
public String getMyName() {
if (++countNameCalled > 10) {
setMyName("ok");
}
return myName;
}
public int getCount() {
return countNameCalled;
}
public void setMyName(String nameIn) {
myName = nameIn;
countNameCalled = 0;
}
public void SayMyNameBean() {}
//the following five methods must be in the Enterprise Bean Class:
public void ejbCreate() {
countNameCalled = 0;
}
//ejbCreate() must match Home Interface's create() method signature
public void ejbRemove() {}
public void ejbActivate() {}
public void ejbPassivate() {}
public void setSessionContext(SessionContext sessionContext) {}
}
A Client Application - SayMyNameClient.java
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
public class SayMyNameClient {
public static void main(String[] args) {
try {
Context initialContext = new InitialContext();
Object initialContextObject =
initialContext.lookup("java:comp/env/ejb/SimpleSayMyName");
SayMyNameHome sayMyNameHome =
(SayMyNameHome)PortableRemoteObject.narrow(
initialContextObject, SayMyNameHome.class);
SayMyName sayMyName = sayMyNameHome.create();
sayMyName.setMyName("Michelle");
String myName = " ";
int myCount = 0;
while (!myName.equals("ok"))
{
myName = sayMyName.getMyName();
//calls stateful session bean which remembers name,
// counts times called
myCount = sayMyName.getCount();
System.out.println("Your name is " +
myName +
" (#" + myCount + ")");
}
sayMyName.remove(); //clean up
} catch (Exception exception) {
System.err.println("Exception!");
exception.printStackTrace();
}
}
}
application-client.xml
A Client JSP - SetMyNameJSP.jsp (run first, then follow link to SayMyNameJSP.jsp)
A Client JSP - SayMyNameJSP.jsp (can run multiple times after SetMyNameJSP.jsp)
web-app.xml
ejb-jar.xml
References
| Sign In |
| to add your own comment |
| Comment by Larry Rate this Comment |
Good catch, looks like I completely neglected to add the ejb-jar.xml file to this example. As you said, the ejb-jar file does have a session-type Stateful tag.
|
| Comment by archive Rate this Comment |
Hi Larry,
|
| Comment by Larry Rate this Comment |
I had to break the href at the end of SetMyNameJSP.jsp into a two part string because search engines were trying to follow the link, and coming up with an error. If you were coding a JSP to actually be used instead of having it's source displayed this wouldn't be necessary. |