Friday, May 31, 2013

EJB-GWT and Netbeans IDE

Accessing EJB from GWT using the Netbeans IDE

I was curious to see how does it work the GWT(google web toolkit) technology, together with EJB-session beans, moreover how the Netbeans development environment uses it's wizards, to ease the development.
I created a MySql table with a simple architecture to demonstrate the functionality, having an ID and a Name field:

My short demonstration is about to visualize the content of the “Name” field by a given record ID.
Netbeans gives us some useful wizards to create entity and session beans to achieve the MySql table's content. Entity beans are a type of Enterprise JavaBean(EJB), a server-side J2EE component, that represents persistent data maintained in a database. Session Beans implements a business task using entity beans. Both are hosted by an EJB container, like the Glassfish server.
To access EJB from a GWT web application, we must create remote session beans. Using the Netbeans Ide, we can achieve this, in following way:
  • first I created a java class library project, remote.jar. This jar file will contain the necessary entity bean for the session bean. Right clicking on this project I added an entity bean with a wizard connecting to the above mentioned MySql table. Additional packages to this project were the EclipseLink JPA 2.1 and Java EE 6 Api Library.
  • in the next step, I created a new project an EJB module. The remote.jar was added to this module as an additional library. Right clicking also on this project, we can call a wizard to create a remote session bean for the entity class. This step, is also generates automatically the remote session bean into the remote.jar file
  • I built both projects and deployed the EJB module using Glassfish 4.
  • To create a GWT web application with Netbeans it is necessary to add the GWT plugin to the IDE from it's menu: Tools-Plugins-Available Plugins or Downloaded Plugins if you have downloaded this plugin from Netbeans' official site. After this, you can create the java web application adding to it the GWT framework, at the end of the wizard.

How to configure the GWT web application to use enterprise java beans

We can do this by the remote.jar package created in the steps above mentioned. After the GWT web application was created, I added the remote.jar package to this web project, with the entity and remote session bean created in it. I don't want to enter in too much details, I suppose that you are familiar with GTW-RPC technology, because this is necessary to reach server-side data. So, after creating the necessary java files to the GWT-RPC(you can use here, also wizards), my server-side implementation of a simple ejb call, looks like the following:

GWT-RPC sends server-side data across the network through serialized objects, therefore one important thing I would like to mention here, that we must create a wrapper class, to send ejb data, across the wire, to the client. An entity record with a given id, is found by the session bean finder method.
The wrapper class implements the Serializable object, gets the entity data as input data into it's constructor, and associates their value to it's local fields, accessed by getter and setter methods.


  1. thank you for sharing
    i came to your blog because i am trying to do the same thing but using requestfactory but i can't find any tutorial using netbeans do you have an idea how to accomplish this?
    thank you

    1. Hi. I think requestfactory has an advantage of its own, that you did not have to think about serialization...On server side, in any servlet, when you would like to invoke the methods of an ejb(session beans) you can use the same anotation keywords like in my example. But, requestfactory has its pattern, of how we should create objects, e.g. at likeness of a database table, so I don't think that is worthy to mix the technology of ejb, entity beans with requestfactory. This is my opinion, but maybe you can look for more information on the net.

    2. I had a problem with validation phase of requestFactory classes I've fixed it by adding the requestfactory-apt.jar to processors-path libararies
      Thank you very much for your attention.

  2. perfect explanation about java programming .its very useful.thanks for your valuable training in chennai | java training in velachery

    1. My pleasure. Glad you found it useful. :)

  3. Very informative ..i suggest this blog to my friends..Thank you for sharingjava training in chennai | java training institutes in chennai