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