I am trying to list down my learnings especially the technical learnings here. It will definitely help in keeping my learnings together and refreshing them when needed. Also It may benefit others having similar issues.

Monday, March 15, 2010

Starting with Google Web toolkit (GWT) in Eclipse

Google Web Toolkit (GWT) is a web development toolkit is an AJAX framework developed by Google. It’s a very good initiative from Google to provide a niche way to implement an AJAX based web application. More details about GWT can be found at Google web site (http://code.google.com/webtoolkit/). To know usability and limitations of GWT, please refer “GWT: Uses and Limitations”.

Installing GWT SDK and Eclipse plug-in is pretty straight forward which is apparent from the steps below.

Extract (unpack) the GWT-x.x.x.zip archive file contents to some folder in the files system e.g. C:\GWT

Add the above folder i.e. C:\GWT into the path variable.

This completes GWT SDK installer. To verify the installation, try opening a command prompt and run the command “webAppCreator”. If it asks for arguments, the installation can be considered as done for now.

Package: the starting package for the web application e.g. com.first.myproject

Check the check box for GWT SDK and make sure the right version is selected.

Uncheck the checkbox for “Google App engine” and click finish.

At this point, “MyFirstProject” is created in the workspace with all required class path defined and a default entry point class named “MyFirstProject.java” in com.first.myproject package.

Right click “MyFirstProject” in workspace and select Google -> GWT compile. It should open a new dialogue showing “MyFirstProject” as entry point. Make sure it’s selected. If required change the logging options, and output style. Click on compile.

It should compile the “MyFirstProject” project without any error and “Compilation succeeded” should appear in workspace console.

To run the application, right click on “MyFirstProject” in work space and select Run As-> web Application. It should start the application in hosted mode. We should see two new windows open. One window is for GWT inline server console and other has the application start page having welcome message and “Click Me” button.

If all above steps passed successfully, congratulations!! Our first GWT web application is created and running in hosted mode at this point.

To run the application on any other web server such as Tomcat, just add the project to the server (if server is running within eclipse) or export the project as WAR file and deploy it on the web server.

Now let’s go through the most important section which is about understanding the GWT application components. We will consider above project as our example.

Understanding the GWT Application components:

If the above created project “MyFirstProject” is opened in navigator view, we can see following files.

Under “src” folder of the project, there will be full package structure i.e. com/first/myproject

Under the last folder i.e. mypoject, we should see two folders namely “client” and “public” and one xml file named as “MyFirstProject.gwt.xml”.

Client package a very important package and it’s used in the client module of the application. All the classes within this package should be self contained and shouldn’t have any reference to classes not available to client server including the server side code of the MyFirstProject project.

MyFirstProject.gwt.xml is the configuration file for the MyFirstProject project and all GWT specific configurations are to be made in the file including the constructs to include certain external files/components into client application.

Public package contains the generated HTML/JavaScript and is used as the starting page of the MyFirstProject application.

MyFirstProject.java extends com.google.gwt.core.client.EntryPoint class implements the method onModuleLoad () which is the starting point of the application.

To understand client/server concept of GWT, let’s create one simple dummy service and make use of it in the section below.

Working with GWT:

Assuming the GWT is installed and first application is successfully created, let’s enhance the “Click Me” button functionality to retrieve the greeting message from the server.

To achieve the above functionality, create an interface called GreetingService in com.first.myproject.client package @RemoteServiceRelativePath ("greet").

Add a method signature as below in GreetingService.java

public String getGreetMessage ();

Create another interface named GreetingServiceAsync in com.first.myproject.client package with a method signature as below:

public void getGreetMessage (AsyncCallback < String > callback);

Create a new package as com.first.myproject.server

Create a GreetingService implementation class named GreetingServiceImpl with a method as below:

public String getGreetMessage () {

String welcomeMessage=”Welcome to the GWT world”;

return welcomeMessage;

}

Note: Service interface and Service Async interface is created in client package while service implementation is created in server package.

Now open previously created entry point class named “MyFirstProject” and change implementation of button.addClickListener(new ClickListener() {.. as below:

This completes the implementation of first asynchronous service call the server on click if “Click Me” button.

From the above example, it’s clear that in GWT application, there is a clear separation on client and server side code. Above all, every call to server is an asynchronous call and all the UI components are created in Java using style elements except the welcome page.