Wednesday, 6 June 2012

GWT Technology


Asynchronous JavaScript and XML (Ajax) development is hard. Not ascending-
Everest hard, maybe not even calculating-your-taxes hard, but hard. This is true for
a number of reasons: JavaScript can require a lot of specialized knowledge and discipline,
browsers have slightly different implementations and feature sets, tooling is
still immature, and debugging in multiple environments is problematic. All of
these factors add up to developers needing a vast knowledge of browser oddities
and tricks to build and manage large Ajax projects.
To help deal with these problems, a number of toolkits and libraries have
emerged. Libraries like Dojo, Script.aculo.us, Ext JS, and the Yahoo User Interface
This chapter covers
■ The history and purpose of GWT
■ The components of GWT
■ GWT basics
■ Working with the GWT shell and GWT compiler


 1 Introducing GWT


Library (YUI) have sought to provide enhanced core features and general ease of use
to JavaScript. In addition, projects like Direct Web Remoting (DWR) have sought to
simplify communications between the client and the server. Even more advanced
techniques, like those used by XML11 and Echo2, create an entire rendering layer in
the browser while executing application code on the server side. These are all valid
approaches, but the Google Web Toolkit (GWT) represents something different.
GWT is a Java to JavaScript cross-compiler. That is, it takes Java code and compiles it
into JavaScript to be run in a browser, as figure 1.1 depicts.
There are many reasons GWT was engineered to start with the statically compiled,
strongly typed Java language, which has generous tooling and testing support, and
then emits JavaScript application versions for all the major browsers in one compilation
step. Chief among these reasons is the simple fact that JavaScript is what is available
in a browser: starting from a single code base and generating all the required
variations makes life a lot easier for the developer, and more consistent, stable, and
performant for the user.
Other aspects that set GWT apart include a harness for debugging Java bytecode
directly as it executes in a simulated browser environment, a set of core UI and layout
widgets with which to build applications, a Remote Procedure Call (RPC) system for
handling communications with a host web server, internationalization support, and
testing mechanisms.
Figure 1.1 An overview of the GWT approach. Java source code is compiled into JavaScript, which is
then run in a web browser as JavaScript/HTML/CSS.


GWT provides a platform for creating true “Rich” Internet Applications (RIAs)—rich in
the sense of allowing the client to maintain state and even perform computations
locally, with a full data model, without requiring a trip to the server for every update to
the interface. This has many advantages for both the user and the developer. The user
gets a more responsive application, and the developer can distribute the load of the
application. GWT also provides a rich platform in terms of a wide variety of UI elements
and capabilities: sliders, reflections, drag-and-drop support, suggest boxes,
data-bound tables, and more. This rich client platform, ultimately utilizing only
HTML, JavaScript, and Cascading Style Sheets (CSS), still has full access, in a variety of
ways, to back-end server resources when needed.
In short, GWT makes Ajax development a lot easier. Not falling-off-a-log easy,
maybe not even taking-candy-from-a-baby easy, but easier—and it makes Ajax applications
better for users. With GWT in Practice, we hope to clarify some facets of GWT that
might seem hard or confusing. Along the way, we’ll also provide practical GWT development
advice based on real-world experience.
If you’re a Java web developer now, you’ll need a change of perspective to build
GWT applications well, and you’ll need a good understanding of the core GWT tools
and how they work in order to fit them into your environment. If you’re an Ajax developer
coming to Java, you’ll need a bit of indoctrination in “The Java Way.” Even if
you’re currently using or experimenting with GWT, you may want to increase your
technical bag of tricks. It’s our hope that you find all of this, and maybe even a little
more, in this book.


1.1 Why GWT




The quick, but wrong, answer to why GWT was created and why it’s gaining popularity is
because it’s new and shiny! Though GWT is no SOA or ESB on the buzz-meter yet, it’s
widely discussed. But does it deserve the attention and praise it receives? When we get past
the hype, what are the reasons for its creation, and why might it make sense to use it?
In the next few sections, we’ll address these questions and lay out the overall
approach of GWT and look at why it was created, where it’s applicable, and why it
should matter to you as a software developer (or manager). We’ll begin our discussion
of what makes GWT significant with a brief trip back through the history of web development,
and the patterns and techniques involved, in order to frame the concepts.


1.1.1 History


In the beginning, there was HTML. Originally, HTML was a semantic document
markup language intended to help researchers on the Internet link related documents
together. But soon after HTML use blossomed, forms came along. When forms
were added to HTML, it transitioned from being strictly a document markup language
to a UI design language. HTML still suffers in some ways from this legacy, but the ease
with which it allowed web-based applications to be deployed became a driving factor
in its use nonetheless.


As the world of web applications began to expand, developers supporting the basic
form applications on the server side repurposed the Model View Controller (MVC) pattern
so that it centered on the server and rendered to HTML. From simple Perl scripts to
complete frameworks, development remained on the server, and things like application
state became a complex problem with complex solutions—involving everything from
Struts to JavaServer Faces (JSF) and Seam. Although these solutions made web applications
more capable and development easier, they didn’t provide the user experience
of a full desktop-like fat client application, and they continued in the same
render-call-render lifecycle that the web had used when forms were first added.
When Netscape introduced JavaScript to the web browser, the browser became
something more than just a simple thin client. It became a platform on its own, capable
of running small applications entirely within the scope of a page. This approach
was widely used for simple things, such as field validation, but its use didn’t spread to
more advanced functionality until Microsoft introduced the XMLHttpRequest (XHR)
object. This object made calls back to the server from the JavaScript environment easy,
and the technique was soon adopted by all the major browsers. The frontiers and
capabilities of web applications expanded even further when dynamic HTML and forms
were combined with the server side via XHR, permitting only portions of browser windows
to be redrawn. The Ajax era was born.


1.1.2 Why Ajax matters


Ajax changed the landscape because it finally broke the browser’s render-call-render
pattern and allowed browsers to update without making a visible, and often slow, trip
back to the server for every page view. Ajax makes even the term page a bit of a relic.
With Ajax, the browser is much closer to being able to support full blown fat or rich
Internet applications.
Because GWT is Ajax and is easily extensible to new browsers, it provides a wider
array of supported devices than many other RIA approaches. Silverlight, Flash, and the
Java Applet Plugin all give developers the ability to create powerful, easy-to-use applications
that run from a web browser, but their dependency on an environment outside
of the browser means that they will always lag behind in deployment. GWT and
other Ajax-based applications worked on Day Zero for both the Nintendo Wii and the
iPhone, for instance. GWT’s concise browser abstraction also makes it easy to update
GWT applications when new versions of currently supported browsers are released,
requiring only a recompile of the Java to support new devices.
Ajax is also significant in that applications that are native to the browser feel more
natural to users. No matter how seamless the browser integration, there are typically
some noticeable differences with plugin technologies. Such differences may include
an install step; the treatment of browser constructs in a different manner, such as with
bookmarks and navigation; and starkly different user interface elements compared
with HTML, CSS, and JavaScript. GWT, and Ajax in general, is a form of RIA that
embraces the parts of the web that do work well and that users are familiar with.


1.1.3 Leveraging the web


Users and developers have embraced the web because it offers centralized management
and delivery, no local installation, instant updates to both functionality and content,
and elements such as shareable bookmarks. Although the specifications that
make up the web have needed to be adapted over the years to cope with drawbacks
such as the stateless nature of HTTP, limited user input elements, and the render-callrender
lifecycle, the advantages of the web have prevailed.
The desirable parts of the web, coupled with the implementation difficulties, are
why RIA technologies exist in the first place, and this includes GWT. Some of these
technologies, though, may go too far in their abstraction of the web layer, hiding the
good as well as the bad.
GWT, happily, is designed with the web tier in mind. The centrally hosted distribution
model that the web offers is fully leveraged, and updates to applications are transparent
and seamless in this model. The URL is the application-download location, and
the browser is the application platform. Concepts that users know and love are also
present, and are intentionally not hidden or disabled. Browser buttons—even the
infamous back button—can be put to work rather than disabled, and can do exactly
what they’re expected to do. Bookmarks are there and, again, they work, even when
deep-linked into a particular area of the application.
Leveraging the parts of the web that do work well is intentional with GWT, as is
GWT’s extension of the RIA landscape to provide tooling and testing support.


1.1.4 Tooling and testing


Another of the reasons GWT is significant and is different from some other RIA offerings
is that it provides tooling and testing support. GWT includes a powerful debugging
shell that allows you to test and debug your code as it interacts with the native
browser on your platform.
The testing support GWT provides is based on JUnit and on a few extensions the
toolkit provides. Your GWT code can be tested as Java, from the shell. After you compile
your code into JavaScript, the same test can be used again in that form by using
further scaffolding provided by GWT. This allows you to test on various browser versions
and, if desired, even on different platform and browser combinations.
The Java language also comes with first-class tooling support. Invaluable tools such
as code parsers like PMD, static analysis tools like Checkstyle and FindBugs, advanced
refactoring engines available in most Java Integrated Development Environments
(IDEs), and debuggers and profilers all function perfectly normally within the context
of the GWT shell.
Tooling support and testing facilities, which are front and center with GWT, are
standard fare in traditional programming but aren’t as common in for client-side web
technologies. Along with this support, GWT provides a great deal of help for developers
in other areas. One of the biggest advantages GWT offers is that it helps you cope
with browser differences.


1.1.5 A single code base


Traditionally, web development has required a doctorate in browserology in order to
cope with all of the differences in behavior among browser types and versions, even
when you’re just writing a standard web application using HTML and forms. Along
with knowing details about multiple versions of HTML, the Document Object Model
(DOM), CSS, JavaScript, and HTTP standards, developers have also needed to be aware
of the way quirks and bugs affect each browser.
Add XML and XHR to the mix, along with more browser differences, and you can
see why Ajax development has a well-deserved reputation on the street as being difficult.
GWT doesn’t avoid that difficulty directly, but it does encapsulate it and allow
developers to worry about their application, rather than about the differences among
browsers. GWT lets you work on a single code base, using Java, and then cross-compile
that code into Ajax-enabled HTML and JavaScript for most of the major browser types
and versions currently in use (Internet Explorer, Firefox, Safari, and Opera at present).
This is a huge benefit to developers. Imagine a world where browser makers actually
adhere to standards and use the latest standard consistently—where you, as a
developer, need create only one version of your application, which will work regardless
of the user agent. That is a fantasy, of course, but the next best thing is to write
application code once and then generate additional code to cope with the differences
in the browser environment. This gives you, in effect, the same thing—one version of
code to write and maintain instead of multiple versions. This is what GWT aims for.
The abstraction isn’t always perfect, but it works well most of the time. And when
there is a problem—a particular browser version has an issue in a particular scenario—
it can usually be resolved quickly with open access to the source and the expertise
of the community (and that expertise is put back into the toolkit, so that others
avoid the same problem in the future).


1.1.6 Limitations


Along with the potentially leaky, but undeniably extremely useful, abstraction of the
web layer, GWT has a few other potential limitations or drawbacks you should be
aware of.
Don’t call GWT a framework (even though in places the documentation does refer
to it as such). GWT hasn’t been here for years, and a lot of components people expect
from “Framework” products aren’t there. Java web frameworks, like Struts and Web-
Work, evolved from the lessons learned in traditional Java web development. As such,
they come with a style, a set of patterns, and expected ways of working, all of which
help developers see where to begin with their applications. GWT doesn’t have that.
When working with GWT as a developer, it’s best to think of it as an analogue to the
Java Abstract Windowing Toolkit (AWT). It’s the basis for your application framework,
not the framework itself.
This can be considered a limitation, but it can also be seen as a well-focused starting
point. GWT doesn’t try to dictate the development approach you’ll use; rather, it
What GWT includes 9
aims to provide the components you can begin with to create Ajax applications, or
frameworks, on your own. Through the course of this book, we’ll demonstrate several
patterns for development that should mitigate concerns in this area; the point here is
that a toolkit isn’t necessarily limiting—it’s a starting point.
One area that is a limitation with GWT is that search engines have a hard time with
JavaScript. Most search engine agents or robots don’t speak JavaScript or know how to
navigate an application that isn’t composed of simple links. This means many GWT
resources are nonexistent as far as such search engines are concerned. Of course, this
applies to all of Ajax and JavaScript in general, not just GWT.
Many techniques can help you cope with this issue. Often the most useful and
thorough is to have a native HTML-only version of your application alongside your
GWT application, so that user agents that can’t load JavaScript, whether or not
they’re search engine agents, can still access your data. The non-JavaScript version
of your application need not be pretty; it should concentrate on core content and
any degree of functionality you can provide. Be careful, though; make sure any non-
JavaScript version of your site is an accurate representation. Providing “search
engine only” content is known as cloaking, and it can get your site banned from
search engine indexes.
When viewed in total, the benefits of GWT outweigh its limitations in many, but not
all, cases. Overall, GWT provides a new approach to building Ajax web applications—
starting from a single code base, generating the variations for all the major
browsers, embracing the concepts of the web that are already familiar to users, and
providing many tools and supporting features along the way.
Now that we have looked at the inspiration and reasoning behind GWT, we’ll turn
to addressing exactly what GWT includes.
Hi Am Back...!!!