GWT — Complete Google Web Toolkit API Reference Guide
Learning Path
flowchart LR
A["Gwt Overview"] --> B["Core Concepts"]
B --> C["Intermediate Topics"]
C --> D["Advanced Topics"]
D --> E["Practical Applications"]
A --> F["You Are Here"]
style F fill:#f90,color:#fff
GWT (Google Web Toolkit) is a development toolkit for building and optimizing complex browser-based applications by compiling Java source code to highly optimized JavaScript that runs in every browser.
What You’ll Learn
By the end of this tutorial, you’ll create GWT applications with EntryPoint classes, build UIs using widgets and panels, communicate with servers via RPC, integrate native JavaScript with JSNI, use UiBinder for declarative layouts, and optimize your app with GWT compiler options.
Why GWT Matters
GWT lets Java developers build complex web applications without writing JavaScript. The GWT compiler produces optimized JavaScript with dead code elimination, inlining, and browser-specific permutations — often outperforming hand-written JavaScript. Google products like AdWords and Google Wallet were built with GWT. While React and Angular dominate modern web development, GWT’s approach inspired frameworks like Vaadin and Elemental2.
Security note: Understanding Reference helps build more secure applications — a core principle at DodaTech, where tools like Durga Antivirus Pro and Doda Browser rely on solid implementation practices.
Entry Point
Every GWT app starts with an EntryPoint class — the main entry point, like Java’s main() method:
public class MyApp implements EntryPoint {
public void onModuleLoad() {
Button button = new Button("Click me");
button.addClickHandler(event -> {
Window.alert("Hello from GWT!");
});
RootPanel.get().add(button);
}
}Line-by-line explanation:
implements EntryPoint— GWT looks for this interface.onModuleLoad()is called when the module loads, similar to a constructor.new Button("Click me")— creates a GWT Button widget (compiled to an HTML<button>element)addClickHandler(event -> { ... })— attaches a click listener using a Java lambdaWindow.alert("Hello from GWT!")— shows a browser alert dialogRootPanel.get().add(button)— adds the button to the page body. Think of RootPanel as the<body>element.
UiBinder — Declarative Layouts
UiBinder separates UI layout (XML) from behavior (Java), similar to HTML + JavaScript separation:
<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'
xmlns:g='urn:import:com.google.gwt.user.client.ui'>
<g:HTMLPanel>
<g:Label text="{handler.name}" styleName="title"/>
<g:Button ui:field="submitButton" text="Submit"/>
</g:HTMLPanel>
</ui:UiBinder>public class MyWidget extends Composite {
interface Binder extends UiBinder<HTMLPanel, MyWidget> {}
private static final Binder binder = GWT.create(Binder.class);
@UiField
Button submitButton;
public MyWidget() {
initWidget(binder.createAndBindUi(this));
}
@UiHandler("submitButton")
void onClick(ClickEvent e) {
Window.alert("Submitted!");
}
}Line-by-line:
@UiField Button submitButton— GWT automatically injects the button from the XML template@UiHandler("submitButton")— connects a click handler to the button without manual wiringGWT.create(Binder.class)— GWT generates the binding code at compile time, not runtime
RPC Service
Remote Procedure Calls let GWT clients talk to Java servers:
// Service interface
@RemoteServiceRelativePath("greet")
public interface GreetingService extends RemoteService {
String greetServer(String name);
}
// Async interface (GWT requires this)
public interface GreetingServiceAsync {
void greetServer(String name, AsyncCallback<String> callback);
}
// Usage in client code
GreetingServiceAsync service = GWT.create(GreetingService.class);
service.greetServer("Alice", new AsyncCallback<String>() {
public void onSuccess(String result) {
Window.alert(result);
}
public void onFailure(Throwable caught) {
Window.alert("Error: " + caught.getMessage());
}
});Line-by-line:
@RemoteServiceRelativePath("greet")— maps the service to a servlet URL (/greet)extends RemoteService— marker interface for GWT RPC servicesAsyncCallback<String>— GWT RPC is asynchronous. The callback handles success or failureGWT.create(GreetingService.class)— GWT generates the proxy code at compile time
JSNI — JavaScript Native Interface
Call JavaScript from Java (and vice versa):
// Call JavaScript from Java
public static native String getBrowserInfo() /*-{
return navigator.userAgent;
}-*/;
// Call Java from JavaScript
public static void showAlert(String msg) {
// This can be called from JSNI JavaScript
}GWT Compiler
The GWT compiler transforms Java to optimized JavaScript:
# Compile for all browsers
mvn compile gwt:compile
# Compile for specific user agent
mvn compile gwt:compile -Dgwt.draftCompile=true
# Super Dev Mode (faster development)
mvn gwt:devmodeCompiler optimizations:
- Dead code elimination — unused classes/methods are removed
- Inlining — small methods are inlined
- Browser-specific permutations — different JavaScript for different browsers
- String interning — repeated strings are shared
Common Mistakes
1. Blocking the UI thread
All GWT code runs in the browser’s UI thread. Long-running synchronous operations freeze the browser. Use Timer or Scheduler for deferred operations.
2. Forgetting the Async interface for RPC
Every RPC service needs a synchronous (for the server) and asynchronous (for the client) interface. The async interface must have the same methods with an AsyncCallback parameter added.
3. Ignoring GWT compiler warnings
Compiler warnings about missing @Override or unused imports may indicate incorrect GWT module configuration.
4. Not using UiBinder for complex UIs
Building complex layouts in Java code creates unmaintainable spaghetti. Use UiBinder XML templates for UI structure.
5. Overusing JSNI
JSNI breaks the Java-only promise of GWT. Use it sparingly — prefer GWT’s built-in cross-browser APIs.
Practice Questions
1. What is the role of the EntryPoint interface?
Answer: EntryPoint is the main entry point for a GWT module. The onModuleLoad() method is called when the module loads, similar to Java’s main().
2. Why does GWT require both a synchronous and asynchronous interface for RPC?
Answer: GWT RPC is asynchronous in the browser. The sync interface defines the service contract; the async interface has the same methods but returns void and takes an AsyncCallback parameter.
3. What is UiBinder used for?
Answer: UiBinder separates UI layout (XML templates) from behavior (Java code), similar to HTML + JavaScript separation for cleaner code.
4. What does the GWT compiler optimize?
Answer: Dead code elimination, method inlining, browser-specific permutations, and string interning — producing JavaScript that often outperforms hand-written code.
Challenge
Build a GWT application that displays a data table with server-side pagination using RPC. Use UiBinder for layout, Cell widgets for the table, and implement sorting and filtering.
FAQ
Try It Yourself
# 1. Create a GWT project using Maven archetype
mvn archetype:generate \
-DarchetypeGroupId=org.gwtproject \
-DarchetypeArtifactId=gwt-archetype-core \
-DarchetypeVersion=2.10.0
# 2. Implement an EntryPoint
# (see the EntryPoint example above)
# 3. Run in Super Dev Mode
mvn gwt:devmode
# 4. Open http://localhost:9876 in your browser
# 5. Click the "Dev Mode On" bookmarklet to debugWhat’s Next
Explore more Java web frameworks:
| Topic | Description |
|---|---|
| https://tutorials.dodatech.com/java/jsf/reference/ | JavaServer Faces component framework |
| https://tutorials.dodatech.com/java/tapestry/reference/ | Apache Tapestry framework |
Related topics to explore:
- Java Development
- JavaScript Interop
- Build & Deploy
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro