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 Salut.java
package firstBean;
import javax.ejb.Remote;

@Remote
public interface Salut{
public String saluta(String nume);
}

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

@Stateless
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 FirstBeanClient.java

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");
System.out.println(beanInstance.saluta(args[0]));
}
}

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

Thursday 11 February 2010

Motivation

I have been asked how is my book going to be different from other Java EE books. The answer is I am going to concentrate on clarifying concepts, rather than giving "how to do" recipes. In my opinion, this approach will prepare the learner to switch between different versions of the platform or of the application server, without major difficulty.

Tuesday 9 February 2010

The first step


I am currently writing a book on programming with Enterprise JavaBeans. I intend to support the readers during their learning process using this blog.
Many thanks to Professor Naotugu Nozue, my main PhD advisor, and his wife, Mrs. Misako Nozue, who encouraged me to achieve this project during their recent visit to Romania. The picture represents Professor and Mrs. Nozue, my friend Mihai and myself at the table where I launched the idea.