about this book

The Google Web Toolkit (GWT) slashes through the issues that surround multibrowser Ajax development. It moves the development lifecycle into the type-safe language of Java while retaining the capability to access JavaScript and third-party libraries. GWT offers the opportunity to develop your Ajax application once for use in multiple browsers and configurations.

GWT in Action aims to give you a solid foundation for developing GWT applications. It puts all the tools and development tasks into the context of typical application development, ensuring that you can understand and avoid the problems faced in GWT development. Throughout the book, the development of a Dashboard application, together with various component applications for the Dashboard, provides the mechanism we use to explain GWT concepts.

We start by providing a solid background on the basics, looking at the tools that are used and where they’re used in a typical development lifecycle. Then, we consider widgets, panels, and events, discussing those provided by GWT and how to create your own (leaning heavily on our experience from developing components for the GWT Widget Library).

Additionally, we tackle some of GWT’s more advanced aspects that are not currently documented in other publications, such as the following:

Writing code that handles internalization and browser differences: Why send IE and Firefox markup for Flash movies when you can send just the appropriate one? And, how can you change the way applications work based on locale as well as changing whole components of your application?

Driving alternatives in application functionality through GWT properties: The Dashboard example comes in two flavors: Internet and intranet versions. Which one is shown to the user is driven by user-defined properties that you’ll define and manage.

Harnessing the powerful GWT generator concept: You’ll learn to introspect classes and tags in comments at compile time to produce new Java classes.

Describing the development of composite objects: You’ll see how to build the EditableLabel we built for the GWT Widget Library. You’ll also construct new composite widgets using other composite widgets—for example, building a complex color picker widget from a number of GWT slider widgets. Finally, you’ll learn how to apply CSS consistently to components.

Integrating with JavaScript through the JavaScript Native Interface (JSNI): We’ll discuss interapplication communication via JavaScript. You’ll build wrappers to simple and complex third-party JavaScript libraries (such as the Google Ajax Search component found in the GWT Widget Library).

Any substantial application requires server-side components, and many books can tell you about all the server-side development techniques (Java, PHP, and so on) for which GWT is highly flexible and which it can plug into. Our approach in GWT in Action is to concentrate several chapters on ensuring you get a thorough understanding of GWT’s client-server communication techniques; for example:

JSON processing using proxy servers (the Yahoo Search component)

GWT-RPC: the technique, the problem, and the solutions (see the Server Status component)

XML processing (used in the menu for the complete Dashboard application)

Form handling, including uploading files

Using traditional Ajax communication

The key point is to get a good understanding of each approach in GWT so you see the flexibility and can choose your server side appropriately (or, if your server side is a given, so you thoroughly understand the technique you’ll be using).

By the end of the book, you’ll understand how the Dashboard application (http://dashboard.manning-sandbox.com) referred to throughout is architected, how it’s constructed, and how it works.

Who should read this book?

The book is aimed at anyone with an interest in GWT. We appreciate that the readership will come from varied backgrounds—JavaScript programmers looking to see what the fuss is all about, Java programmers learning that they can now program Ajax applications simply, server-side developers interested in understanding GWT-RPC, web designers looking to understand what this useful maturing of development means to them, and many others.

Readers looking for a gentle introduction to GWT concepts and components will appreciate the easy way in which these topics are introduced. The book has been particularly designed to reduce the large number of gotchas that are found when you first look at GWT. More advanced readers will find that the book contains many aspects you have perhaps thought of but not yet figured out how to implement—and, we hope, a few you haven’t thought of!

You should be familiar with the concept of Java classes and packages, although we feel this is something you can pick up as you read the book, follow the code samples, and use an IDE. A lot of GWT (and Java) issues revolve around classpaths and GWT’s package structure, so we recommend a thorough reading of chapter 9 if you’re getting stuck.

Roadmap

Chapter 1 introduces GWT and examines where it sits in relation to complementary and competing technologies. It also shows how easy it is to get a first GWT application up and running.

Chapter 2 provides a detailed understanding of the steps required to build the default GWT application using the GWT command-line tools, indicating what each tool is, why it’s used, and when you should use it. This chapter also discusses alternative approaches to creating your application, including by hand and by using an IDE wizard.

Chapter 3 is the first step you’ll take away from the default GWT application and toward the initial version of the Dashboard. We’ll explain what default files you need to change/replace and why.

Chapter 4 starts our discussion of standard GWT components. It looks at widgets, but not in a textbook style. Using component applications from the running Dashboard application, you’ll get insight into the use of key widgets from GWT. The second part of the chapter looks at building your own widgets, including the GWT Widget Libraries PNGImage widget and two widgets that extend the standard MenuItem widget.

Chapter 5 covers panels, looking at how they’re used in the Dashboard and how to extend and create your own panels—including the DashboardPanel used for the Dashboard application components.

Chapter 6 introduces event handling as performed by GWT and explains how to harness it for your own components. You’ll see how to handle new events for widgets as well as plumb together the event handling for double-clicks and so on.

Chapter 7 finishes the four key aspects of GWT application components by thoroughly discussing the development of composite widgets. We lean on the EditableLabel, which has been around for nearly a year and is included in the GWT Widget Library. You’ll also build some slider composite widgets, culminating in a sliding color-picker widget.

Chapter 8 is where you’ll learn how to harness any JavaScript library you may have and want to interact with. The GWT Widget Library includes a GWT widget that wraps the Google Ajax Search functionality; you’ll learn how we built that component as well as how to wrap the Google Video Search component.

Chapter 9 wraps up the direct user interface components included in the book. You’ll learn the details of using the GWT module’s XML configuration file to, among other things, inject resources, alter the project layout, invoke class replacement and generation, and include server components. You’ll also see how to include third-party GWT libraries, as well as how to create you own libraries of GWT code.

Chapter 10 takes you into the world of GWT-RPC, where you’ll learn how to pass Java objects between the web browser and your Java servlets.

Chapter 11 expands on the previous chapter by showing you common usage patterns and custom serialization for GWT-RPC. This include polling techniques, including how to emulate server-push.

Chapter 12 looks at GWT’s support for classic Ajax and HTML forms. These tools offer flexibility, allowing your GWT application to connect to any server-side application. This chapter provides real-world examples for loading external configuration data and using GWT to upload files to the server.

Chapter 13 finishes our discussion of client-server communication with GWT’s support of the JavaScript Object Notation (JSON) message format. We’ll explain what JSON is and provide an example of using it to communicate with the Yahoo Search API.

Chapter 14 looks at GWT’s powerful generators. You’ll learn how to build generators that introspect code at compile time to generate new subclasses with additional functionality. You’ll also see how these generators can promote comments written in code to be displayed in dialogs to the user at runtime.

Chapter 15 rounds off the advanced techniques by thoroughly covering properties, including internationalization both in the normal sense of changing text for labels and menus, and so on, and also in terms of changing whole components of your application based on the defined locale. You’ll also use properties to drive the selection of the view that is presented to the user.

Chapter 16 shows you how to test your GWT code with JUnit and how to deploy your finished application to the server. You’ll learn how to organize your deployed code to reduce clutter on the server.

Chapter 17 completes the book by investigating the underlying mechanisms of GWT, for those interested in delving a little deeper. You’ll see how bootstrapping works (including the changes introduced by GWT 1.4), what your compiled code should look like, and what the various output files produced by the compiler are related to.

Code conventions and downloads

This book contains copious amounts of code and examples. The whole substantial GWT application, called the Dashboard, is referred to throughout the book and is available from www.manning.com/hanson or www.manning.com/GWTinAction. The additional libraries that are used by the application code and that you need to download separately are as follows:

JSON classes used in the server-side code, from http://JSON.org.

File upload server-side processing from Apache Commons (commons-fileupload.jar, commons-io.jar, commons-codec.jar). Apache Commons is at http://jakarta.apache.org/commons/.

The Apache Commons commons-httpclient component, which the proxy server implementation used in one component relies on.

GWT Widget Library (http://gwt-widget.sourceforge.net/).

Source code in listings or in text is in a fixed-width font to separate it from ordinary text. Additionally, Java method names, component parameters, object properties, and HTML and XML elements and attributes in text are also presented using fixed-width font. Java method names generally don’t include the signature (the list of parameter types).

Java, HTML, and XML can all be verbose. In many cases, the original source code (available online) has been reformatted, adding line breaks and reworking indentation, to accommodate the available page space in the book. In rare cases, even this wasn’t enough, and listings include line-continuation markers. Additionally, comments in the source code have been removed from the listings.

Code annotations accompany many of the source code listings, highlighting important concepts. In some cases, numbered bullets link to explanations that follow the listing.

GWT was originally a closed-source development program, but it’s now open-source. You can download the binary packages for your platform (Windows, Linux, Mac OX) from here: http://code.google.com/webtoolkit/versions.html. If you’re interested in contributing to the platform or living on the bleeding edge of development, then you can grab the source code from the SVN archive here: http://code.google.com/webtoolkit/makinggwtbetter.html.

Author Online

The purchase of GWT in Action includes free access to a private web forum run by Manning Publications, where you can make comments about the book, ask technical questions, and receive help from the authors and from other users. To access the forum and subscribe to it, point your web browser to www.manning.com/GWTinAction or www.manning.com/hanson. This page provides information on how to get on the forum once you are registered, what kind of help is available, and the rules of conduct on the forum.

Manning’s commitment to our readers is to provide a venue where a meaningful dialogue between individual readers and between readers and the authors can take place. It’s not a commitment to any specific amount of participation on the part of the authors, whose contribution to the forum remains voluntary (and unpaid). We suggest you try asking the authors some challenging questions lest their interest stray! The Author Online forum and the archives of previous discussions will be accessible from the publisher’s web site as long as the book is in print.

About the authors

Robert Hanson is a senior Internet engineer specializing in Java application development and maintenance. Robert is the creator of the popular open source GWT Widget Library found at http://gwt-widget.sourceforge.net and also maintains a blog at http://roberthanson.blogspot.com where he talks about GWT and other topics relating to the industry. You can contact him at iamroberthanson@gmail.com.

Adam Tacy works as a project manager at WM-data in the Nordics, specializing in delivery of new/leading-edge projects while enjoying the associated risks and need to establish repeatable processes. He was a (grateful) early adopter of GWT and has contributed to the GWT Widget Library. In his spare time, you can find him falling through ice, mishandling kite-surf equipment, and enjoying all things Norwegian, Swedish, and Finnish while missing good old British bacon and beer. You can contact him at adam.tacy@gmail.com.