Sei sulla pagina 1di 8

EJB Application

1) New EJB Module

2) Selecting Glassfish Server

3) Create a New Package called test under the Source Package Folder.
Create a new interface called "Home".under test package and add the code.
HomeImpl.java :

package test;
import javax.ejb.Stateless;
@Stateless
public class HomeImpl implements Home{
@Override
public String sayHello(String name) {
return "Welcome to EJB"+" "+name;
}
}

4) Create a SessionBean class which implements this Interface .


HomeImpl.java :

package test;
import javax.ejb.Stateless;
@Stateless
public class HomeImpl implements Home{
@Override
public String sayHello(String name) {
return "Welcome to EJB"+" "+name;
}
}

5) Create a GlassFish Descriptor File

6) Set the JNDI Name for the HomeImpl Bean by editing the file or by clicking the TAB "EJB" and
giving jndi name as "abc".
7) Right click on the Project Node and select clean and Build.
After clean and build again right click on the project node and select "Deploy".
8) RightClick on the Project Node (MyApp) and create new Class called "Main"

Main.java :

package test;

import java.util.Properties;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;

public class Main {

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


{
Properties p=new Properties();
InitialContext c=new InitialContext();
Object l=c.lookup("abc");
Home r=(Home)l;
System.out.println(r.sayHello("Ayaz"));
}
}
Output :

Potrebbero piacerti anche