Providing current date and time using Stateless session bean



Development of every bean includes minimum 4 programs.

  1. Remote interface
  2. Home interface
  3. Bean program
  4. Client program

Remote interface defines all the methods (business methods) used for communication

Home interface defines methods used to connect to the bean. Bean program is the implementation program for all themethods of remote and home interfaces. Finally, a client program that invokes the business methods of the bean working on the server.

1st program: Remote interface

import javax.ejb.*;     //for EJBObject

import java. rmi.*;  // for RemoteException

//every business method should throw RemoteException

public interface Time extends EJBObject

{

String printTime() throws RemoteException;

}

2nd program : Home interface

import javax.ejb.*;

import java.rmi.*;

public interface TimeHome extends EJBHome

{

public Time create()throws CreateException, RemoteException;

//the create () method should return the interface object that extends //EJBObject

}

3rd program : Bean class file

import javax.ejb.*;

import java.util.*;   // for Date Class

public class TimeBean implements SessionBean

{

public String printTime()

{

//this is the implementation of business method

String str = “Time is “+new Date().toString();

return str;   // str is returned to client

}

public void ejbCreate() { } //this corresponds to create() method of home interface

//override the callback methods of SessionBean interface

public void ejbPassivate() { }

public void ejbActivate() { }

public void ejbRemove() { }

public void ejbSessionContext(SexxionContext ctx) { }

}

Note:

  1. Every Bean should implement either SessionBean or EntityBean interfaces
  2. For every create() method of Home interface, there should be a corresponding ejbCreate() method. create() method can be overloaded and in this example we have only one

4th program : Client program:

import java.rmi.*;

import javax.naming.*;

import java.util.*;  //for Properties data structure

public class HelloClient

{

public static void main(String args[])throws Exception

{

Properties p=new Properties();

p.put(Context.INITIAL_CONTEXT_FACTORY, “weblogic.jndi.T3InitialContextFactor”);

p.put(Context.PROVIDER_URL, “t3 :// 197.0.0.102.7001″);

Context ic=new InitialContext(p); //SNRao is reference(jndi)name given inXML file

TimeHome home1= (TimeHome) ic.lookup(“SNRao”);

Time hello1=home1.create(); //this is the create() method of home interface

String retval =hello1.pringTime();  // now printTime() is executed on server and //output is sent to client

System.out.println(retval);

hello.remove();

}

}

Random Posts

  • Define HashSet Class
  • Springs Interview Questions
  • Define Session Hijacking
  • EJB Interview questions
  • JSP Interview Questions

Post a Comment