Building Web Application with Google Web Toolkit 2
The GWT Components
Compiler
is a Java-to-JavaScript compiler. It takes your Java 2.5 code and produces distinct equivalent JavaScript versions that can be run on all supported browsers. The compiler does many optimizations, the most important ones are:
- Dead Code Elimination: The code that is never called, isn't included in the output file.
- Constant Folding: When the value of an expression can be known at compile time, the expression is calculated beforehand, and the result will be directly used. This will make the execution faster.
- Copy Propagation: It is an extension of Constant Folding, example if a=5 and b=a*a+5, the second part (b) will be compiled as b=230.
- String Interning: To avoid creating the same strings over and over again, each distinct string is created once and used everywhere.
- Code Inlining: For short, simple methods, GWT substitutes the calling methods by its actual code.
In Relation to optimization, there are some points to be taken into consideration while writing Java Code:
- JavaScript doesn't have 64-bit integer numeric type, so GWT emulates long variables with a pair of 32-bits integers. This works properly but is noticeably slower. Also, when you use JSNI, you cannot pass these variables to JavaScript routines.
- For floating point numbers, JavaScript provides only a 64-bit (double) type, which implies that overflows and result precision in arithmetic operations won’t be exactly the same as in Java. Also, the strictfp keyword is disregarded.
- Exceptions are also handled differently. In JavaScript, most of the Java produced exceptions (such as NullPointerException or MemoryOverflowException ) are replaced by a JavaScriptException. This causes a problem: When running in development mode, a NullPointerException will be thrown, and you need to catch (NullPointerException e) but in compiled mode, you need to catch (JavaScriptException e) and you duplicate your exception handling code. Another option, of course, is just to catch (Exception e) and then check for the class of the exception.
- JavaScript provides no multithreading, so all thread-related functions will either be ignored or rejected.
JRE Emulation Library
GWT provides a partial implementation of JRE, it is called the JRE Emulation Library.
Supported Java packages (but not fully):
- java.io
- java.lang
- java.sql
- java.util
The reason why not all classes are supported is because JavaScript cannot use some of them. For example, because JavaScript cannot use files, most of the classes in java.io just wouldn’t work when compiled into JavaScript.
There are also certain GWT packages that provide extra functionality (that maybe missing from Java Libraries):
- com.google.gwt.i18n.client.DateTimeFormat and com.google.gwt.i18n.client.NumberFormat for formatting numbers.
- com.google.gwt.core.client.Duration for timing purposes.
- com.google.gwt.user.client.Random substitutes java.util.Random.
- com.google.gwt.user.client.Timer used instead of java.util.Timer.
As a general rule, before relying on any specific class or exception, check whether it is actually implemented or its just a placeholder needed for JRE compatibility, or its trimmed down, limited, version of the usual JRE version.
UI Library
GWT provides a large, standard set of widgets (such as buttons or text input fields) and panels. Using widgets is quite similar to Swing, so Java programmers can feel at home; however, note that there are no layout managers (discussed next) and panels or CSS are used instead for positioning objects.
Panels are containers for widgets or other panels. Panels also do double-duty as layout managers; for example, FlowPanel uses standard HTML flow rules (or Swing’s FlowLayout’s ) , where as VerticalPanel stacks its elements vertically.
Project Structure
The required directories are:
- src directory: this directory is further divided into sub-directories:
- client: is the client side code, that runs on the user's browser.
- shared: shared code that is used by both the client and server side code.
- server: is the code that runs on the server side.
You can also have other directories for client-side code. However, you need to include these directories in the <source> element in the Module Descriptor (that is discussed later on in this page). On the other hand serve side code has to be located under the server directory.
- test and gwttest directories: are used for testing (such as using JUint).
- war directory: the output code resides there. It is structured to comply with Java Web Servers.
GWT Module and XML Module Descriptors
A module in GWT is an encapsulation of a functionality. Each module is defined by a XML descriptor file. The XML descriptor file ends with the extention .gwt.xml.
GWT Internationalization i18n and Localization l10n
Internationalization
is the process of designing a software application so that it can be adapted to various languages and regions without engineering changes (Wikipedia).
Localization
is the process of adapting internationalized software for a specific region or language by adding locale-specific components and translating text (Wikipedia).
References
- Essential GWT: Building for the Web with Google Web Toolkit 2: http://www.informit.com/store/product.aspx?isbn=9780321705143
- Google Web Toolkit: http://code.google.com/intl/de/webtoolkit/
- Wikipedia Internationalization and Localization: http://en.wikipedia.org/wiki/Internationalization_and_localization
