GWT with EJB

Using GWT with EJB

Google Groups
Subscribe to Cypal Studio for GWT
Visit this group

GWT addresses the web layer and EJB, the middle layer, its natural to use both of them to have a neat J2ee app. In this small write-up I'll explain how to use them together.

I assume that you know how to create EJB project and Web project with Eclipse WTP and Cypal Studio for GWT. 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 don't clearly understand the above statement, then the rest of this writeup is for you.

Assume that there is a text box and a button in a web page. When you type a person’s name in the textbox and press the button, the age of the person should be shown to the user.

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 …; 


The EJB looks something like:

public class AgeServiceBean implements SessionBean {
//All EJB members here 
          public int getAge(String name){ 
               int age; 
                      age = 18; //Pranni is always 18 :-)
              else if(name.equals(”GWT”)) 
                     age = 1;    // GWT still young!
                     age = 40; // default value return age; 

I opted for a very simple implementation. Of course you can do the normal way of contacting the Data Layer. (Entity beans/plain JDBC/Hibernate). Check whether name is null or not, throw a NameNotFound exception. Its the plain old EJB code. GWT does not restrict anything here. If you already have some EJB, exposing it to an existing servlet or a swing client, very well, you can reuse that also.

Now let us create a Remote service in the Web layer.

The GWT’s RemoteService is very similar to the RemoteInterface of EJB:

public interface AgeServiceGWT extends RemoteService{

    public int getAge(String name);

    //other members like Util, SERVICE_URI etc ...  


The RemoteService’s Impl is the key which connects GWT and EJB:

public interface AgeServiceGwtImpl
    extends RemoteServiceServlet implements AgeServiceGwt{

    public int getAge(String name){

        Context ctx = new InitialContext();
        AgeServiceHome home = (
        AgeServiceEjb ageService = home.create();
        return ageService.getAge(name);


Again, you can do all the normal EJB client stuff here. Add parameters to create initial context, configure the JNDI name, cache the Home interface, etc. If you have ever used JSP/servlets calling EJB code, think it in the same way. Treat the Impl class as a servlet (in fact it is!) and do the usual way.

Common problems:

  • 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.