- XML Reference Guide
- Overview
- What Is XML?
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Table of Contents
- The Document Object Model
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- DOM and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- Implementations
- DOM and JavaScript
- Using a Repeater
- Repeaters and XML
- Repeater Resources
- DOM and .NET
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Downloads
- DOM and C++
- DOM and C++ Resources
- DOM and Perl
- DOM and Perl Resources
- DOM and PHP
- DOM and PHP Resources
- DOM Level 3
- DOM Level 3 Core
- DOM Level 3 Load and Save
- DOM Level 3 XPath
- DOM Level 3 Validation
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Implementations
- The Simple API for XML (SAX)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- SAX and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- SAX and .NET
- Informit Articles and Sample Chapters
- SAX and Perl
- SAX and Perl Resources
- SAX and PHP
- SAX and PHP Resources
- Validation
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Document Type Definitions (DTDs)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XML Schemas
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- RELAX NG
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Schematron
- Official Documentation and Implementations
- Validation in Applications
- Informit Articles and Sample Chapters
- Books and e-Books
- XSL Transformations (XSLT)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XSLT in Java
- Java in XSLT Resources
- XSLT and RSS in .NET
- XSLT and RSS in .NET Resources
- XSL-FO
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XPath
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XML Base
- Informit Articles and Sample Chapters
- Official Documentation
- XHTML
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XHTML 2.0
- Documentation
- Cascading Style Sheets
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XUL
- XUL References
- XML Events
- XML Events Resources
- XML Data Binding
- Informit Articles and Sample Chapters
- Books and e-Books
- Specifications
- Implementations
- XML and Databases
- Informit Articles and Sample Chapters
- Books and e-Books
- Online Resources
- Official Documentation
- SQL Server and FOR XML
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Implementations
- Service Oriented Architecture
- Web Services
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Creating a Perl Web Service Client
- SOAP::Lite
- Amazon Web Services
- Creating the Movable Type Plug-in
- Perl, Amazon, and Movable Type Resources
- Apache Axis2
- REST
- REST Resources
- SOAP
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- SOAP and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- WSDL
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- UDDI
- UDDI Resources
- XML-RPC
- XML-RPC in PHP
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Ajax
- Asynchronous Javascript
- Client-side XSLT
- SAJAX and PHP
- Ajax Resources
- JSON
- Ruby on Rails
- Creating Objects
- Ruby Basics: Arrays and Other Sundry Bits
- Ruby Basics: Iterators and Persistence
- Starting on the Rails
- Rails and Databases
- Rails: Ajax and Partials
- Rails Resources
- Web Services Security
- Web Services Security Resources
- SAML
- Informit Articles and Sample Chapters
- Books and e-Books
- Specification and Implementation
- XML Digital Signatures
- XML Digital Signatures Resources
- XML Key Management Services
- Resources for XML Key Management Services
- Internationalization
- Resources
- Grid Computing
- Grid Resources
- Web Services Resource Framework
- Web Services Resource Framework Resources
- WS-Addressing
- WS-Addressing Resources
- WS-Notifications
- New Languages: XML in Use
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Google Web Toolkit
- GWT Basic Interactivity
- Google Sitemaps
- Google Sitemaps Resources
- Accessibility
- Web Accessibility
- XML Accessibility
- Accessibility Resources
- The Semantic Web
- Defining a New Ontology
- OWL: Web Ontology Language
- Semantic Web Resources
- Google Base
- Microformats
- StructuredBlogging
- Live Clipboard
- WML
- XHTML-MP
- WML Resources
- Google Web Services
- Google Web Services API
- Google Web Services Resources
- The Yahoo! Web Services Interface
- Yahoo! Web Services and PHP
- Yahoo! Web Services Resources
- eBay REST API
- WordML
- WordML Part 2: Lists
- WordML Part 3: Tables
- WordML Resources
- DocBook
- Articles
- Books and e-Books
- Official Documentation and Implementations
- XML Query
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XForms
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Resource Description Framework (RDF)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Topic Maps
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation, Implementations, and Other Resources
- Rich Site Summary (RSS)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Simple Sharing Extensions (SSE)
- Atom
- Podcasting
- Podcasting Resources
- Scalable Vector Graphics (SVG)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- OPML
- OPML Resources
- Summary
- Projects
- JavaScript TimeTracker: JSON and PHP
- The Javascript Timetracker
- Refactoring to Javascript Objects
- Creating the Yahoo! Widget
- Web Mashup
- Google Maps
- Indeed Mashup
- Mashup Part 3: Putting It All Together
- Additional Resources
- Frequently Asked Questions About XML
- What's XML, and why should I use it?
- What's a well-formed document?
- What's the difference between XML and HTML?
- What's the difference between HTML and XHTML?
- Can I use XML in a browser?
- Should I use elements or attributes for my document?
- What's a namespace?
- Where can I get an XML parser?
- What's the difference between a well-formed document and a valid document?
- What's a validating parser?
- Should I use DOM or SAX for my application?
- How can I stop a SAX parser before it has parsed the entire document?
- 2005 Predictions
- 2006 Predictions
- Nick's Book Picks
Javascript is an easier language to learn and use than Java.
Right?
Well, kind of. True, JavaScript is easier to use than Java because it's not compiled and you can use it in the browser and all that, but if you're trying to build complex web-based applications, there's just something about it that makes it a pain in the backside. Maybe it's the lack of debugging, or the fact that it is, essentially, a procedural language, no matter how object-oriented you try and make it.
On the other hand, Java gives you debugging and natural object oriented-ness, but using it in the browser isn't a trivial task.
But what if you could do both? What if you could write your applications in Java and then port them over to JavaScript with the touch of the button? And what if that converted JavaScript was cross-browser, JDK-independent, serverless code? yes"> Wouldn't that be lovely?
You bet it would. And that's what you get with the Google Web Toolkit, an interesting little tool that's been quietly making the rounds. It seems complicated when you first try and use it because all of the official documentation is reference-based rather than tutorial-based, but once you get the hang of it, it's quite simple, really.
I've spent the weekend delving into this little beauty, and I've got to tell you, it's delicious.
The toolkit works like this: you create an application using the provided script. That application includes a small HTTP server and specialized browser. These two pieces enable you to write Java code that represents what should happen in the browser, save it, and run it in the browser. (No, I didn't forget to compile it. You actually run it without specifically stopping to compile.) When the application is complete, use the provided script to compile it into JavaScript you can include on your page. Period.
Let's take a look at how that works.
- Start by downloading the toolkit, which is available at http://code.google.com/webtoolkit/download.html.
- Unzip it into the directory of your choice and navigate to its root directory in a command window.
- Create a new application. You have the choice of creating an application to edit by hand, or of
creating a project you can import into the Eclipse IDE for actual debugging, and even
JUnit testing. To keep things simple,
we'll use a straight application:
>>>applicationcreator com.informit.client.GiftDemo Created file C:\gwt-windows-1.0.21\src\com\informit\GiftDemo.gwt.xml Created file C:\gwt\gwt-windows-1.0.21\src\com\informit\public\GiftDemo.html Created file C:\gwt\gwt-windows-1.0.21\src\com\informit\client\GiftDemo.java Created file C:\gwt\gwt-windows-1.0.21\GiftDemo-shell.cmd Created file C:\gwt\gwt-windows-1.0.21\GiftDemo-compile.cmd
These five pieces are a complete (though simple) application. Starting at the bottom, theGiftDemo-compile.cmd
script compiles the application into its final JavaScript form when you're finished working on it. TheGiftDemo-shell.cmd
script enbles you to run the application, as we'll see in a moment.GiftDemo.java
is the actual Java code. (Note that the tool will force you to useclient
as the final component of your package name, even though the overall toolkit doesn't require it.)Giftdemo.html
is the HTML page hosting the resulting Javascript, andGiftDemo.gwt.xml
is a descriptor file that tells the toolkit what's going on. - OK, now we're ready to see it in
action. Run the
GiftDemo-shell.cmd
script. You should see two windows pop up, including a specialized browser, as seen in Figure 1:
If you click the “Click me” button, you'll see “Hello World” text appear next to it. Click it again, and the text disappears. Not a tremendously complex application, surely, but it's someplace to start.
If we look at the GiftDemo.java
file, we see it's fairly straightforward:
package com.informit.client; import com.google.gwt.core.client.EntryPoint; import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.ClickListener; import com.google.gwt.user.client.ui.Label; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.Widget; /** * Entry point classes define <code>onModuleLoad()</code>. */ public class GiftDemo implements EntryPoint { /** * This is the entry point method. */ public void onModuleLoad() { final Button button = new Button("Click me"); final Label label = new Label(); button.addClickListener(new ClickListener() { public void onClick(Widget sender) { if (label.getText().equals("")) label.setText("Hello World!"); else label.setText(""); } }); // Assume that the host HTML has elements defined whose // IDs are "slot1", "slot2". In a real app, you probably would not want // to hard-code IDs. Instead, you could, for example, search for all // elements with a particular CSS class and replace them with widgets. // RootPanel.get("slot1").add(button); RootPanel.get("slot2").add(label); } }
Starting at the top, notice that Google has created its own implementation of various classes such as buttons, text labels, and various form elements, as well as more Java-ish classes like Panel. This is just a tiny subset. You can use any Java code or objects in your Java application, but not everything is translatable into JavaScript, so try to stick with the GWT classes as much as possible, or at least with the translatable classes listed at http://code.google.com/webtoolkit/documentation/jre.html.
Next we have the class itself, which implements the
EntryPoint
interface. Any class that
serves as the “main base,” so to speak, of an application, must implement
EntryPoint
, and as such, the onModuleLoad()
method, which defines what happens
when the user pulls up the page.
In this case, what happens is that we create a Button
and a
Label
object. The button is an actual
HTML button, rather than a Java emulation of an HTML button, but rather than
specifying its behavior using the onclick
handler, we can do it in true Java
style, adding a listener object.
The listener object implements the onClick()
method, which
in this case checks the contents of the label and reacts accordingly.
Of course, so far we haven't actually added either object to
the page. To do that, we'll use the
add()
method for the RootPanel
, which represents the page itself.
Ultimately, the toolkit gives you enormous
power in laying out objects, including panel types that lay sequentially added
widgets out horizontally, vertically, or in a grid.
Some panel types, which we'll see later, include functionality
that's a nightmare to do on your own. For example, the TabPanel easily builds a tab-based page, where content
appears based on the tab clicked by the user. (Check out the sample applications that come with the toolkit for
an idea of what you can do.)
You also have the ability to drop widgets into specific
places on your page. For example, here
we see that we're specifically requesting elements with an id value of slot1
and slot2
. The GiftDemo.html
page
includes HTML that references those values:
<h1>GiftDemo</h1> <p>This is an example of a host page for the GiftDemo application. You can attach a Web Toolkit module to any HTML page you like, making it easy to add bits of AJAX functionality to existing pages without starting from scratch.</p> <table align=center> <tr> <td id="slot1"></td> <td id="slot2"></td> </tr> </table>
In part 2 we'll get into the actual coding of an
application, using Ajax, and so on, but before we do that, we need to know how
to export this into its pure JavaScript form.
To do that, we use the GiftDemo-compile.cmd
script:
>>>>GiftDemo-compile Output will be written into C:\gwt-windows-1.0.21\www\com.informit.GiftDemo Copying all files found on public path Compilation succeeded
This process drops about a dozen files into the
com.informit.GiftDemo
directory, which you can then copy, lock, stock, and
barrel, to your web server. You can also
view them directly in your actual browser, as opposed to the special browser
that comes with the toolkit.
Next up, we create a new application that demonstrates some of the cool capabilities of GWT, such as manipulatable pages and even easy -- very easy! -- Ajax.