The recently published Google Web Toolkit (GWT) is a comprehensive set of APIs and tools that enable users to create dynamic WEB applications almost entirely using Java™ code. Philip McCarthy back to his popular Ajax series for Java developers, showing you what GWT can do and helping you determine if it's right for you.
GWT (see Resources) takes an unusual approach to WEB application development. Instead of using normal isolation from the client and server-side code base, it provides a Java API that allows the creation of component-based GUIs and then compiles them to display them on the user's Web browser. Compared with the general WEB application development experience, using GWT is closer to using Swing or SWT for development, and it is also trying to abstract the HTTP protocol and the HTML DOM model. In fact, the application will eventually almost always be rendered in a Web browser.
GWT implements these capabilities through code generation, which uses its compilers to generate JavaScript from client Java code. GWT supports a subset of the Java.lang and java.util packages and also supports the APIs provided by GWT itself. The compiled GWT application consists of HTML, XML, and JavaScript fragments. However, these fragments are hard to distinguish, so it's a good idea to think of a compiled application as a GWT equivalent of a black box--java bytecode.
In this article, I'll create a simple GWT application that uses the program to get weather reports from the remote Web API and display it in the browser. Throughout the process, I'll briefly describe as many GWT features as possible, and I'll mention some potential problems that you might encounter.
From a simple start
Listing 1 shows the Java source code for the simplest application that can be made with GWT:
Listing 1. The simplest example of GWT
public class Simple implements EntryPoint {
public void onModuleLoad() {
final Button button = new Button("Say 'Hello'");
button.addClickListener(new ClickListener() {
public void onClick(Widget sender) {
Window.alert("Hello World!");
}
});
RootPanel.get().add(button);
}
}
This code looks much like GUI code written using Swing, AWT, or SWT. As expected, listing 1 creates a button that displays the message "Hello world!" when the button is clicked. The button is added to the Rootpanel, which is a GWT wrapper object that wraps around the body of the HTML page. Figure 1 shows the case when the application is running in the GWT Shell. The GWT Shell is a debug hosting environment (Debugging hosting environment) contained in the GWT SDK, combined with a simple browser.
Figure 1. Running the simplest GWT example