January 15, 2008

GWT: A new way of doing Web development

Author: Federico Kereki

Are you dazzled by the way you can drag Google Maps around or move from one place to another without having to reload the screen? Or maybe you're a fan of Gmail and its look and feel? If you want to develop Web sites with Google's signature user-friendly features but are afraid of the work involved, take a look at the Google Web Toolkit (GWT).

GWT, a tool for Web programmers, made its first public appearance in May 2006 at the JavaOne conference. It's in full development, and its current version is 1.4.61; version 1.5 is promised for Q1 2008. It's licensed under the Apache License 2.0, though some of its components are under other licenses.

Why would you use GWT?

Developing a modern, dynamic Web application can be a frustrating experience. For starters, you need to write specific code to address the many incompatibilities among browsers. Otherwise, your site might look fine in, say, Mozilla Firefox, but not work at all in Internet Explorer or Safari. Web developers usually end up spending more time on cross-testing than on the actual development of the site itself. Also, to develop interactive sites, you need to use JavaScript, a language that's powerful but lacking in modularity and testing facilities, which causes problems when creating large systems. Of course, you also have to know HTML and Cascading Style Sheets (CSS) for the Web design itself, and languages such as PHP or Java for writing Web services. Many sites require Dynamic HTML (DHTML), so you'll have to change the actual source code of the page you're seeing on the fly, and there are some compatibility issues there too. Finally, you might need to do international development for a multilanguage site -- another complication.

124163-1-thumb.png

GWT lets you avoid these problems by allowing you to work in Java, using tools such as Eclipse or NetBeans for coding, Unified Modeling Language (UML) tools for class design, JProfiler for optimizing performance, JUnit for automated testing, and Javadoc for documentation. With these tools you can avoid common errors such as typos and type mismatches, and you can use refactoring for code quality enhancement. You can run or debug your application in hosted mode, meaning your program runs as Java code within the Java Virtual Machine (JVM), giving you access to debugging tools. You can then convert it to JavaScript and HTML and run it in Web mode, in the same way your end users would.

Note that you never have to worry about HTML or JavaScript. When GWT compiles from Java to JavaScript, it takes care of compatibility problems, so your site looks the same no matter which browser you use. GWT also greatly simplifies the use of AJAX, allowing you to easily access Web services that might or might not also be written in Java.

What does GWT include?

GWT includes four components:

  • Hosted Web browser: Allows you to test your Java application the same way end users would see it.
  • Web interface library: Lets you create and use Web browser widgets, such as labels, text boxes, and radio buttons. You do your Java programming using these widgets, and the compilation process transforms them into HTML equivalents.
  • Java emulation library: Provides JavaScript-equivalent implementations of the most common Java standard classes. (Note that not all of Java is available, and there are restrictions as to which classes you can use. It's possible that you'll have to roll your own code, should you want to use an unavailable class.)
  • Java-to-JavaScript compiler (translator): Produces the final Web code.

If you're like most programmers, you probably worry about the performance of the converted code. GWT generates ultracompact code that you can further compress and cache, so your end users can download a few dozen kilobytes of end code just once. The quality of the generated code approaches (and perhaps even surpasses) the quality of hand-written JavaScript, especially for larger projects. According to the GWT creators, version 1.5 is going to be even better. Finally, being able to avoid wasting time doing debugging in every Web browser gives you more time for application development itself, which allows you to produce more features and better applications.

Installing and using GWT

Before installing this toolkit, you should already have installed the Java Development Kit (JDK). GWT is currently oriented to working with Eclipse, and that's what Google's own developers use, so you might want to get that as well. (You can also get GWT to work with NetBeans or other common development environments.) Then go to the download site, get the latest package -- it's about 25MB in size -- and extract it by using tar jxf ../gwt-linux-1.4.61.tar.bz2. No further installation is required, and you can use GWT from any directory.

To create a new project, follow the GWT's instructions. If you're working with Eclipse, follow these instructions:

  • Create a directory for your project.
  • Create a project in the directory, using ./pathToGWT/projectCreator -eclipse MyOwnProject.
  • Create a basic empty application with ./pathToGWT/applicationCreator -eclipse MyOwnProject com.yourCompanyName.client.YourApplicationName.
  • Open Eclipse, go to File -> Import, choose Existing Projects Into Workspace, and select the directory in which you created your project.

You can now edit both the HTML and Java code, add new classes, and test your program in hosted mode. When you're satisfied with the final product, compile it (an appropriate script is generated when you create the original project) and deploy it to your Web server.

Conclusion

GWT is an innovative way of doing Web development. It allows Java programmers to produce Web applications, use the tools they're accustomed to, and work at a higher level, while producing modern, highly interactive Web sites.

Categories:

  • Programming
  • Internet & WWW