GWT with EJB
Posted On Oct 13, 2006 at by Prakash G.R.A better and updated version is maintained here (http://grprakash.googlepages.com/gwtwithejb).In this post, I thought I'll explain how to use GWT with EJB. GWT addresses the web layer and EJB the middle layer, its natural to use both of them to have a neat J2ee app. I assume that you know how to create EJB project and Web project with Googlipse. For the second one, I've written a post here. If you are an advanced EJB developer and have used GWT for a while, "Use the RemoteServiceImpl as a EJB client and continue in the as usual way". If you are not, then the rest of the post is for you. What we are trying now is to have a text box in the web page and a button. When you key in a person's name there and press the button, it will show the age of the person. The first step is to create a Stateless Session Bean. The Remote & Home interface will look like this:
public interface AgeServiceEjb
extends EJBObject, Remote {public int getAge(String name);
}public interface AgeServiceHome
extends EJBHome {
public AgeService create()
throws ...;
}
public class AgeServiceBean
implements SessionBean {//All EJB members here
public int getAge(String name){int age;if(name.equals("Pranni"))
age = 18; //Pranni is always 18 :-)
else if(name.equals("Bill Gates"))
age = 51;
else
age = 40; // default valuereturn age;
}
}
public interface AgeServiceGwt
extends RemoteService {public int getAge(String name);
// Other members Util, SERVICE_URI etc
}
public interface AgeServiceGwtImpl
extends RemoteServiceServlet
implements AgeServiceGwt{public int getAge(String name){
Context ctx = new InitialContext();
AgeServiceHome hme =
(AgeServiceHome)ctx.lookup("<JNDI name>");
AgeServiceEjb ageService = hme.create();
return ageService.getAge(name);
}
}
How do I deploy the EJB in the GWT's embedded Tomcat server?
You can't. In fact you can't deploy it even on an external Tomcat. Its just a web container. You need an app server like WebLogic or JBoss
How do I create a EAR file?
Create an EAR project and add these projects (EJB & Web) to it. Select File->Export->Export as EAR file
Can I deploy the application in an app server and still debug in hosted mode?
Yes. Look at "Deploying to an external server" in the GWT tutorial. Its the same way.
I really liked it. Very good job!
Is it possible to expose domain objects as well. In your example the value that is returned to client is simply an integer. Is is possible to return let's say typical domain model object, such as customer, address, etc?