Thursday, 18 February 2010

Lesson One: Build and Test a Simple Session Bean in 8 Easy Steps

A session bean implies a class and an interface.

1. Define the interface in a file named
package firstBean;
import javax.ejb.Remote;

public interface Salut{
public String saluta(String nume);

2.Define the class in a file named
package firstBean;
import javax.ejb.*;

public class SalutEJB implements Salut{
public String saluta(String nume){
return "Salut,"+nume+"!";}
3. Compile the class and the interface
>set CLASSPATH=.;C:\Sun\SDK\lib\javaee.jar
>javac -d . firstBean/*.java

4.Write a client for the session bean, in a file named

package firstBeanClient;

import firstBean.Salut;
import javax.naming.InitialContext;

public class FirstBeanClient{
public static void main(String[] args) throws Exception
InitialContext context = new InitialContext();
Salut beanInstance = (Salut) context.lookup("firstBean.Salut");

5.Compile the client
>javac -d . firstBeanClient/*.java

6.Build a jar archive containing the session bean (class and interface)
>jar cvf FirstBeanJar.jar firstBean\

7.Load the archive (FirstBeanJar.jar) to the application server (I use GlassFish and its graphical administration console)

8. Execute the client including appserv-rt.jar and javaee.jar in the classpath.
>java -classpath C:\Sun\SDK\lib\appserv-rt.jar; ->
->C:\Sun\SDK\lib\javaee.jar; ->
C:\EJB firstBeanClient.FirstBeanClient Cristina

No comments: