Java .

Java Stateful Session EJB

Note: This Example Uses J2EE 1.3

The Remote Interface -

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;
To download source right-click here and "Save As...".

The Home Interface -

import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;

public interface SayMyNameHome extends EJBHome {
   SayMyName create() throws RemoteException, CreateException;
To download source right-click here and "Save As...".

The Enterprise Bean Class -

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) {
      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) {}
To download source right-click here and "Save As...".

A Client Application -

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 = 
	  SayMyNameHome sayMyNameHome = 	
              initialContextObject, SayMyNameHome.class);	
          SayMyName sayMyName = sayMyNameHome.create();
          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) {	
To download source right-click here and "Save As...".


A Client JSP - SetMyNameJSP.jsp (run first, then follow link to SayMyNameJSP.jsp)

To download source right-click here and "Save As...".

A Client JSP - SayMyNameJSP.jsp (can run multiple times after SetMyNameJSP.jsp)

To download source right-click here and "Save As...".




J2EE (TM) 1.3 Tutorial from Sun
Enterprise Java Beans by Richard Monson-Haefel
Mastering Enterprise JavaBeans by Ed Roman
Comments Comments are left by visitors to and may or may not be accurate.
Comment by Larry on 2006-08-22 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.

I see what you mean about jspInit being run only once. Maybe the bean initialization should be moved out of there. I had this example working on a machine two laptops and a desktop ago, so it will probably be a while before I can work on any improvements.

Thanks for the great questions!

Comment by archive on 2006-08-10 Rate this Comment

Hi Larry,

I was trying to get my head around your example above.

1. I thought that a session ejb had to have a


tag in the ejb-jar.xml file to make it stateful.

2. It looks like the example puts a reference to the remote interface into the session, this is then available to SayMyName.jsp for the rest of the session.

But because jspInit is only run once no matter who invokes the jsp, all users will be saving a reference to the same remote interface in their sessions.

Please could you clarify these 2 points.

Comment by Larry on 2006-05-19 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.

Sign in to comment on Java Stateful Session EJB.