June 12, 2006

Step #2: A Calculator which retrieves data from a Java server process

(Updated 6/14 with links to the two parts of this step)

The next step in building a real world mortgage calculator using GWT is to retrieve something simple from a back end. (See the problem's introduction and Step #1, as well as the client code and the server code portions of the step described below.) It will be easiest to start with something simple. In this example, the client will pull two different mortgage interest rates (a 30 year fixed rate and a 5/25 ARM rate) from the Expresso back end. Since I am accessing a Java back end, there are two options.

  • Google's services infrastructure. The benefit of this is fairly painless and transparent marshaling of Java value objects--that's according to the documentation, I haven't used it. The main downside is that I can't use Expresso's default servlet (with its authentication, logging, and caching) to handle the service request, because you must always extend Google's RemoteServiceServlet.
  • Use the HTTPRequest class. This class essentially wraps the XMLHttpRequest object in a cross browser way, so if you're familiar with that Javascript construct (which is at the heart of AJAX), the API shouldn't be too shocking. This class limits the types of response available; no XML/DOM tree is passed back from a request, just text (in a String). The benefit of this method is that it's very familiar to folks who've used XMLHttpRequest and is relatively simple. The main downside is that you're limited to Strings as return values. There are, however, ways around that limit.

Based on the current requirements, it made more sense to use HTTPRequest than Google services. I could see using the Google services layer if I were doing some green field development in Java, or in a situation where such transparent marshaling saved a significant deal of work.

Of course, when you're sending back text, you have a couple of options for encoding the data. I considered a custom encoding, but that's not very scalable to large datasets, escaping and unescaping can be non trivial, and there are well known text transfer formats out there. Of those I know, XML and JSON are the primary ones. I went with JSON because Google kindly (almost) provides a JSON parsing library in their sample code. This parsing library is nice because it allows me to send back values as Strings, Arrays, Booleans, Numbers or Objectss and converts the JSON to the appropriate type. I say almost because I had to make a few changes to their code.

In my next post, I'll look at the server side changes I made, including integrating the JSON.Simple Java library into Expresso.

Posted by moore at June 12, 2006 04:33 PM
© Moore Consulting, 2003-2006