Professional Documents
Culture Documents
Overview
The purpose of this training is to provide an introduction to some of the fundamental concepts of
Google Web Toolkit (GWT). By going through the steps in developing a simple Snake and
Ladder game, the trainee will learn the following:
• How to install GWT
• How to use GWT’s command-line tools
• Understand GWT’s project structure
• Working with GWT’s widgets and panels
• Communication with server using simple RPC (with RequestBuilder)
• Running GWT application in Hosted and Web mode
Prerequisites
Some programming experience with the Java language and Swing/AWT
Some usage experience with the Eclipse IDE will be great
Page 1 / 23
code::XtremeApps::
Google Web Toolkit Training
1.) Sun Java Standard Edition SDK (JDK 5.0 Update 12)
Download Link: http://java.sun.com/javase/downloads/index_jdk5.jsp
Installation Instructions: http://java.sun.com/j2se/1.5.0/install.html
2.) Google Web Toolkit Version 1.4 (Build 1.4.60 - released on August 28, 2007)
Download Link: http://code.google.com/webtoolkit/versions.html
Installation Instructions: http://code.google.com/webtoolkit/gettingstarted.html
Please note that using a Java IDE like Eclipse is not a must for GWT development, but using
one will definitely make your development effort a more enjoyable and fruitful experience. As
GWT ships with a command line utility that can automatically generate Eclipse project files,
naturally, we will use Eclipse as our development environment. Note that all the sample project
codes distributed with the training materials are also Eclipse-based.
You should be able to follow the installation instructions given in the above URL links for the
installation of the tools and software without any difficulty. But since this is training on GWT, I
will mention briefly about the installation of GWT (which are also available online from GWT’s
Getting Started Guide):
1.1.3. GWT does not have an installer application. All the files you need to run and use GWT
are located in the extracted directory. The diagram below shows the list of folders and
files located in the extracted directory (for Windows):
Page 2 / 23
code::XtremeApps::
Google Web Toolkit Training
1.1.4. Take note of the two executable files in the extracted directory: applicationCreator
and projectCreator, which we will be using very soon to create our first GWT
application.
1.2.2. Then generate our GWT application using the applicationCreator script, noting that
we use an additional -eclipse flag specifying the name of our Eclipse project (as
specified in the previous step). In our case, the name of the entry-point Java class to
be created is “org.itsc.client.SnakeAndLadder”. Note that this tool enforces the best
practice of using “client” as the final package for our entry-point class.
Page 3 / 23
code::XtremeApps::
Google Web Toolkit Training
1.2.3. The diagram below shows the eventual GWT application directory structure generated
as a results of the above two steps.
Page 4 / 23
code::XtremeApps::
Google Web Toolkit Training
1.3.2. With respect to our GWT skeleton application generated (SnakeAndLadder), let’s take
a look at the purpose of some of the generated files:
SnakeAndLadder.gwt.xml - This is the configuration file for the module that is used to
define the entry-point class, dependencies and compiler directives. The entry-point
class is the class that is executed when the browser loads the module.
SnakeAndLadder.html - This is the HTML file that will load and execute our
SnakeAndLadder application.
SnakeAndLadder.java - This is the sample entry-point class where we will place our
Java codes and these Java codes will be compiled into JavaScript.
1.3.3. Now let us try launching our application by running the SnakeAndLadder-shell.cmd
shell script. Alternatively, we can run the SnakeAndLadder-compile.cmd shell script
and then open the file SnakeAndLadder.html (generated into www directory) in our
browser.
Page 5 / 23
code::XtremeApps::
Google Web Toolkit Training
1.3.4. Try clicking on the “Click me” button which toggles the visibility of the “Hello World!”
message
Page 6 / 23
code::XtremeApps::
Google Web Toolkit Training
SnakeAndLadder.gwt.xml - This is the configuration file for the module that is used to
define the entry-point class, dependencies and compiler directives. The entry-point
class is the class that is executed when the browser loads the module.
Page 7 / 23
code::XtremeApps::
Google Web Toolkit Training
SnakeAndLadder.html - This is the HTML file that will load and execute our
SnakeAndLadder application.
Page 8 / 23
code::XtremeApps::
Google Web Toolkit Training
SnakeAndLadder.java - This is the sample entry-point class where we will place our
Java codes and these Java codes will be compiled into JavaScript.
Adding a ClickListerner to
the “Click me” button, so that
the button can respond to a
mouse click event on it.
GWT applications construct user interfaces using widgets that are contained within panels.
Widgets and panels work the same way on all browsers; by using them, you eliminate the need
to write specialized code for each browser. Examples of widgets include Button, TextBox, and
Tree. But you are not limited to the set of widgets provided by the toolkit. There are a number
of ways to create custom widgets yourself. Panels, such as DockPanel, HorizontalPanel, and
RootPanel, contain widgets and are used to define how they are laid out in the browser.
Page 9 / 23
code::XtremeApps::
Google Web Toolkit Training
Page 10 / 23
code::XtremeApps::
Google Web Toolkit Training
Page 11 / 23
code::XtremeApps::
Google Web Toolkit Training
1.5.2. The java source file SnakeAndLadder.java is updated to reflect our main GUI design.
The listing below shows where some of the codes updates are done. For the complete
program source code up till this stage, please refer to the Eclipse project:
SnakeAndLadderPrj-02.
Do note that we have not added any listener to the widgets yet, which means the widgets e.g.
buttons, will not react to any UI events e.g. button click.
Page 12 / 23
code::XtremeApps::
Google Web Toolkit Training
1.5.3. Notice that we have placed the image file boardImage.jpg showing the Snake And
Ladder game board into the “src/org.itsc/public/img” directory. This file is used in the
creation of the Image widget.
1.5.4. We can make use of Cascading Style Sheets (CSS) defined in the application’s CSS
file to define the widgets’ associated visual styles. In our case, the
SnakeAndLadder.css CSS file is created in the “src/org.itsc/public” directory with the
following content.
1.5.5. In addition to creating the application CSS file, we will also need to update the module
file SnakeAndLadder.gwt.xml so that GWT is kept informed about this dependency.
Page 13 / 23
code::XtremeApps::
Google Web Toolkit Training
Note that this will be longest section of the entire training guide as we will also be introducing
two additional Java classes to our project to support the game logic, namely: Player.java and
SnakeAndLadderMap.java. We will give brief explanations of the purpose of these two
classes but will not go into the details (as these are more game logic codes and not really
GWT-dependent). For the complete program source code up till this stage, please refer to the
Eclipse project: SnakeAndLadderPrj-03.
Key steps:
• We called the addClickListener() method to add a ClickListener to the button.
• For code simplicity, the ClickListener object is created as an anonymous class within
the method call itself (the codes highlighted in blue).
• We implemented the onClick() method and added codes to handle the event where the
button was clicked.
• The codes within onClick() are game logic codes dealing with mostly initializations stuff
e.g. players’ turn, players’ position on game board etc.
• One thing to note is that as boardPanel is an instance of AbsolutePanel, we can set the
players icons (Image widget) position at any location on the boardPanel by using the
setWidgetPosition() method and passing the XY coordinates as part of the parameters
list.
• We can set the content of a TextArea by using the setText() method.
Page 14 / 23
code::XtremeApps::
Google Web Toolkit Training
1.6.2. Similarly, we have added a ClickListener to the “Roll Dice” button but the codes within
the onClick() method is more complicated due to the numbers of tasks that need to be
performed when this button is clicked. The following steps look at a few code segments
within this method.
1.6.3. GWT’s Random and Timer class used to emulate dice roll animation.
Key points:
• Note that GWT Random and Timer class are defined in com.google.gwt.user.client
package.
• Note that we cannot use the Random class defined in the usual java.util package as
this class is not part of GWT’s JRE Emulation Library.
• For the full list of classes and methods that are available in GWT’s JRE Emulation
Library, you can refer to: http://code.google.com/webtoolkit/documentation/jre.html.
• Remember that GWT’s client-side code will eventually be compiled into its equivalent in
JavaScript.
• Notice also the use of CSS to set the visual styles for the dice buttons.
• The Timer class is a simplified, browser-safe timer class. This class serves the same
purpose as java.util.Timer, but is simplified because of the single-threaded
environment.
Page 15 / 23
code::XtremeApps::
Google Web Toolkit Training
Page 16 / 23
code::XtremeApps::
Google Web Toolkit Training
1.6.5. The Player class file is design to be a template for creating player objects. The class
members included are: an Image widget to represent the player icon appearing on the
game board and the player positional information.
In addition, the following piece of code segment within the Player class will create a
PopupPanel whenever there’s a MouseOver event for the player’s Image widget. It will
also capture MouseMove events to move the PopupPanel with the mouse.
1.6.6. Finally, the SnakeAndLadderMap class file basically allows the application to know the
locations of all snakes and ladders.
A HashMap from the java.util package is used to store the starting and ending points of
the snakes and ladders. Note that we can use the HashMap class direct from the usual
package as this class is part of GWT’s JRE Emulation Library.
Page 17 / 23
code::XtremeApps::
Google Web Toolkit Training
When a web application needs to interact with a backend server (for e.g. to load or save data),
it will need to make a client-side request (from the browser) to the server. The mechanism for
interacting with a server across a network is called making a remote procedure call (RPC).
While processing a RPC, the server is executing server-side code. As mentioned in GWT
Reference Guide: GWT does not meddle with your ability to run Java bytecode on your server
whatsoever. Server-side code doesn't need to be translatable, so you're free to use any Java
library you find useful.
In the context of AJAX-based web applications, modern browsers include a special JavaScript
object called the XMLHttpRequest object that allows asynchronous communication between
the browser and the server. This asynchronous mode of communication allows e.g. updating a
portion of a web page without forcing a page refresh like traditional HTTP request/response
communication model.
GWT provides two mechanisms for RPC that sit on top of the XMLHttpRequest object. The first
mechanism is based on the RequestBuilder class, which is essentially a wrapper around the
XMLHttpRequest object. The second mechanism is GWT-RPC, which is a more
comprehensive mechanism that gives you much more flexibility.
Page 18 / 23
code::XtremeApps::
Google Web Toolkit Training
For example, RequestBuilder allows only exchange of text between the client and server. If
support of complex data structure is required, custom serialization and deserialization codes
must be written. GWT-RPC, on the other hand, allows you to send any serializable Java
objects (with support for automatic serialization and deserialization).
In our case, as implementing GWT-RPC is beyond the scope of this training targeted at
beginners, we will implement a simple scenario of client-server communication using the
RequestBuilder class.
1.7.2. Next, we add a ClickListener object for the “WoW” (Words of Wisdom) button and
within its onClick() method, we add the code to create a RequestBuilder object pointing
to our WordsOfWisdomServlet.
Page 19 / 23
code::XtremeApps::
Google Web Toolkit Training
1.7.3. Note that the last parameter to the RequestBuilder constructor is the string
“wordsofwisdom”. This is actually the servlet name that will be mapped to the
org.itsc.server.WordsOfWisdomServlet which we have created earlier. This mapping
information need to be manually defined in the module file SnakeAndLadder.gwt.xml.
1.7.4. Since this will be an asynchronous communication, the requester will not wait for a
response from the server before continuing with its execution. Thus, when the
requester sends out its request, it will need to assign a callback handler (a Java object)
which will be responsible in handling the response when it arrives. In GWT, we use the
RequestCallBack class to create such a callback handler.
Referring to the listing below, note that we will need to implement the onError() and
onResponseReceived() methods to handle both situations. Another thing to note is
that even when a response is successfully received, we still need to check the returned
status code to ensure that it is 200 – which indicates a successful response.
A “Word of Wisdom” retrieved from the Response object will then be displayed in a
DialogBox.
For the complete program source code up till this stage, please refer to the Eclipse
project: SnakeAndLadderPrj-04.
Page 20 / 23
code::XtremeApps::
Google Web Toolkit Training
1.7.5. To emphasize the asynchronous aspect of the communication, we purposely delay the
response by a few seconds (on the server-side). Note that during these few seconds of
delay, you can still continue with your game of Snake and Ladder.
Page 21 / 23
code::XtremeApps::
Google Web Toolkit Training
You will spend most of your development time working in hosted mode, which means
that you are interacting with your GWT application without it having been translated into
JavaScript. Anytime you edit, run, and debug applications from a Java integrated
development environment (IDE), you are working in hosted mode. When running in
hosted mode, the Java Virtual Machine (JVM) is actually executing your
application code as compiled Java bytecode, using GWT plumbing to automate
an embedded browser window. By remaining in this traditional "code-test-debug"
cycle, hosted mode is by far the most productive way to develop your application
quickly.
In hosted mode, since the code runs in the JVM, that means when your code performs an action
like handling a mouse event, you get full-featured Java debugging, with exceptions and the
advanced debugging features of IDEs like Eclipse we are using now.
As for running of your application in Web mode, GWT Developer Guide states:
As you move from development into end-to-end testing and production, you will begin
to interact with your application in web mode more often. Web mode refers to
accessing your application from a normal browser -- where it runs as pure
JavaScript -- as it is ultimately intended to be deployed.
To create a web mode version of your module, you compile it using either the
"Compile/Browse" button available in the hosted browser window or the command-line
compiler com.google.gwt.dev.GWTCompiler.
Web mode demonstrates what makes GWT unusual: when your application is
launched in web mode, it runs completely as JavaScript and does not require any
browser plug-ins or JVM.
Page 22 / 23
code::XtremeApps::
Google Web Toolkit Training
1.8.2. Note that after the GWT compilation process, the compiled code will be place in a
“www” directory within the Eclipse project.
Conclusions
Finally, we have come to an end to this training. It is hoped that you will find this training a
useful experience in learning a few fundamentals of GWT development. However, there are
still many GWT development concepts which are not covered in this short training. To further
your GWT knowledge, please refer to the following references which I have found very useful
and informative during the course of my preparation of this training guide.
References:
• Official Google Web Toolkit Developer Guide
http://code.google.com/webtoolkit/documentation/com.google.gwt.doc.DeveloperGuide.html
• Official Google Web Toolkit Class Reference
http://code.google.com/webtoolkit/documentation/gwt.html
• GWT in Action, authored by Robert Hanson and Adam Tacy
http://www.manning.com/hanson/
• Agile AJAX : 36 GWT Tutorials
http://blogs.pathf.com/agileajax/2007/07/36-gwt-tutorial.html
• Google™ Web Toolkit Solutions: Cool & Useful Stuff, by David Geary
http://safari.awprofessional.com/0131584650?tocview=true
Page 23 / 23