Using Google Web Toolkit with NetBeans
Java
Saturday, April 19, 2008 by Lluis Turró - 1436 reads

A week a go I found the incredible Google Web Toolkit and decided to develop XMLPortal administration with it. Since I'm using NetBeans, the first thing was looking for NB support on GWT. This directed me to the always interesting Roumen's blog. Unfortunately NB module doesn't support server-side code and I had to configure by hand the new GWT library. This is how I did it:

Creating the project

First thing was to create a new library project in NetBeans. Selected File -> New Project... -> General -> Java Library. Once the library created, I moved to the library folder and ran the GWT command applicationCreator mypackage.MyMainClass. This command-line utility generates a starter application, a really simple one.

In case you don't have a web application project, create one. This blog concerns to adding the GWT library to any web application.

Add the GWT library to the web project. This ensures the generated jar files are deployed.

Creating the deploy command-line

In this example the web application and the GWT library are both pending from the same folder.

In the GWT library project's root, along with [ProjectName]-compile, create this script, named as [ProjectName]-deploy:

#!/bin/sh

APPDIR=`dirname $0`;



$APPDIR/XPAdministrator-compile



rm -Rf $APPDIR/../[web_application]/web/[GWT_folder]*

cp -R $APPDIR/www/*.* $APPDIR/../[web_application]/web/[GWT_folder]
For Windows users: rm -Rf deletes recursively a folder and cp -R copies recursively.

Creating the libraries

Create the GWT library with: [GWT_folder]/gwt-dev-linux.jar] and [GWT_folder]/gwt-user.jar]==

Now, copy [GWT_folder]/gwt-user.jar] into [GWT_folder]/gwt-user-noj.jar]. Open the late with your favorite zip tool, I used Ark . Remove all the javax packages, they offend Tomcat and the library wouldn't load.

Create the GWT-NOJ library with: [GWT_folder]/gwt-user-noj.jar]

The GWT library must be added to the GWT library project, the GWT-NOJ to the web application project.

Server side code, the GWT servlets

For each servlet you need to provide the corresponding entries in the web application's web.xml.

Running the web application

Before running the web application you need to run the above created script [ProjectName]-deploy. This step ensures the GWT compiled java classes get into the web application root.

Dealing with web application contexts

There is a gap in GWT regarding web contexts. You are requested to provide a ServiceEntryPoint for remote calls. The problem here is that you don't know in advance on which context the application will be deployed. And service entry point requires the web context to be specified.

This is the problematic code:


  ServiceDefTarget endpoint = (ServiceDefTarget)adminService;

  endpoint.setServiceEntryPoint("/servlet_name");

The above works good is the application is deployed at the root context, but fails otherwise.

A solution to web application contexts

Store a cookie with the application context. How to do this is up to you, I used the context servlet of XMLPortal.

Now the problematic code can be rewritten as:


  ServiceDefTarget endpoint = (ServiceDefTarget)adminService;

  endpoint.setServiceEntryPoint(new Cookies().getCookie("cookie_name") + "/servlet_name");

Caveats

CVS can drive you crazy since GWT compiler results are a letter soup. Because this I'm copying compiled results into [project]/build/web instead of [project]/web. This saves me from CVS nigthmare. Maybe you would like to do the same .