Apr 052008
 

Java didn’t make it big on browsers. Javascript (no relationship with Java) is the defacto standard that all browsers support.

Javascript is an interpreted, dynamic typed language with powerful features not available in mainstream languages: functions are first class (they can be passed around like any other object); it has associative arrays and closures. However, because it is dynamically typed, one cannot take advantage of IDEs such as eclipse or netbeans which provide powerful features such as Intellisense. Furthermore, powerful static type checking that is available for statically typed languages such as C and Java cannot be employed with Javascript. Static type checking can flag several potential problems at compile time.

Google’s Web Toolkit (GWT) elegantly solves this problem, by allowing you have your cake and eat it too, and Java gets a second chance as it becomes the language of choice for developing apps that run on the browser. The twist is that instead of Java being compiled to bytecode, it is compiled to Javascript. There are several advantages:

  • There are lots of Java programmers who can now develop rich AJAX apps without having to learn Javascript.
  • Static Java type checking will catch errors at compile time that might otherwise cause runtime Javascript errors.
  • They can use familiar IDE’s like eclipse and netbeans. They can debug and single step through the Java code. JUnit can be used for testing.
  • They can test from the IDE using hosted mode browser.
  • User’s are familiar with the browser’s back button and instead of discouraging users from using it, GWT handles it correctly.
  • They don’t need to worry about the quirks of different browsers such as Firefox, Mozilla, IE, Safari, Opera etc. GWT takes care of it by generating code specific to the browsers.
  • GWT is efficient in that only Javascript code specific to the browser will be sent over the wire. The Javascript is cached and no code is downloaded unless the application has been updated on the server.
  • If required, you can specify that GWT obfuscate the generated Javascript code. This also reduces the size of the code that needs to be downloaded to the browser.
  • Let Google do the job of keeping up to date with handling browser quirks and fixing bugs. All you need to do is to install GWT updates when they become available.
  • Powerful extensions like GWT-ext provide a powerful library with widgets such as trees, chart, tooltips, drag and drop, grids and more.

If required, you can embed Javascript in Java using JSNI. This is analogous to using JNI. Though this should be avoided as it may not be portable across browsers. This is useful if you need to integrate with existing Javascript code.

Using GWT, I developed an interactive mortgage and “Loan you can afford” calculator. The Mortgage calculator is different in that you get the results as you type: there is no need to click “Enter” or the “Calculate” button. You can also enter a payment and see its effect on the loan period, The “Loan you can afford” calculator takes the monthly payment one can afford and shows in one screen the loan amounts for several interest rates at once. Given that all the calculations are done in the browser, the results are instantaneous. You can access my GWT application at http://mortgage-calc.srinivasan.biz. Check it out and give me your comments.

The application mentioned above doesn’t make any calls to the server. For applications that need to talk to the server, rpc mechanisms are provided. The server side is typically Java (running on tomcat or equivalent). However, you can also use Rails instead of Java. The GWT-on-Rails project integrates GWT client side compiled Javascript with Rails REST web services. For this, you need GWT-REST which is an asynchronous RESTful client implementation for GWT.

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>