{"id":351,"date":"2006-06-12T16:33:59","date_gmt":"2006-06-12T22:33:59","guid":{"rendered":"http:\/\/www.mooreds.com\/wordpress\/?p=351"},"modified":"2007-01-27T19:05:27","modified_gmt":"2007-01-28T01:05:27","slug":"step-2-a-calculator-which-retrieves-data-from-a-java-server-process","status":"publish","type":"post","link":"https:\/\/www.mooreds.com\/wordpress\/archives\/351","title":{"rendered":"Step #2: A Calculator which retrieves data from a Java server process"},"content":{"rendered":"<p>(<em>Updated 6\/14 with links to the two parts of this step<\/em>)<\/p>\n<p>The next step in building a real world mortgage calculator using GWT is to retrieve something simple from a back end.  (See the <a href=\"http:\/\/www.mooreds.com\/weblog\/archives\/000349.html\">problem&#8217;s introduction<\/a> and <a href=\"http:\/\/www.mooreds.com\/weblog\/archives\/000348.html\">Step #1<\/a>, as well as <a href=\"http:\/\/www.mooreds.com\/weblog\/archives\/000353.html\">the client code<\/a> and <a href=\"http:\/\/www.mooreds.com\/weblog\/archives\/000352.html\">the server code<\/a> 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.<\/p>\n<ul>\n<li><a href=\"http:\/\/code.google.com\/webtoolkit\/documentation\/com.google.gwt.doc.Develop%20erGuide.RemoteProcedureCalls.html\">Google&#8217;s services infrastructure<\/a>.  The benefit of this is fairly painless and transparent marshaling of Java value objects&#8211;that&#8217;s according to the documentation, I haven&#8217;t used it.  The main downside is that I can&#8217;t use Expresso&#8217;s default servlet (with its authentication, logging, and caching) to handle the service request, because <a href=\"http:\/\/code.google.com\/webtoolkit\/documentation\/com.google.gwt.doc.Develop%20erGuide.RemoteProcedureCalls.CreatingServices.html\">you must always extend Google&#8217;s <code>RemoteServiceServlet<\/code>.<\/a><\/li>\n<li>Use the <a href=\"http:\/\/code.google.com\/webtoolkit\/documentation\/com.google.gwt.user.client.HTTPRequest.html\">HTTPRequest<\/a><br \/>\nclass.  This class essentially wraps the <a href=\"http:\/\/developer.apple.com\/internet\/webcontent\/xmlhttpreq.html\">XMLHttpRequest object<\/a> in a cross browser way, so if you&#8217;re familiar with that Javascript construct (which is at the heart of AJAX), the API shouldn&#8217;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 <code>String<\/code>).  The benefit of this method is that it&#8217;s very familiar to folks who&#8217;ve used XMLHttpRequest and is relatively simple.  The main downside is that you&#8217;re limited to <code>String<\/code>s as return values.  There are, however, ways around that limit.<\/li>\n<\/ul>\n<p>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.<\/p>\n<p>Of course, when you&#8217;re sending back text, you have a couple of options for encoding the data.  I considered a custom encoding, but that&#8217;s not very scalable to large datasets, <a href=\"http:\/\/www.mooreds.com\/weblog\/archives\/000292.html\">escaping and unescaping can be non trivial<\/a>, and there are well known text transfer formats out there. Of those I know, <a href=\"http:\/\/www.w3.org\/XML\/\">XML<\/a> and <a href=\"http:\/\/www.json.org\/\">JSON<\/a> 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 <code>String<\/code>s, <code>Array<\/code>s, <code>Boolean<\/code>s, <code>Number<\/code>s or <code>Objects<\/code>s and converts the JSON to the appropriate type.  I say almost because I had to make a few changes to their code.<\/p>\n<p>In my next post, I&#8217;ll look at the server side changes I made, including  integrating the <a href=\"http:\/\/www.json.org\/java\/simple.txt\">JSON.Simple Java library<\/a> into Expresso.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>(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 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,6],"tags":[],"class_list":["post-351","post","type-post","status-publish","format-standard","hentry","category-gwt","category-programming"],"_links":{"self":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/351","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/comments?post=351"}],"version-history":[{"count":0,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/351\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/media?parent=351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/categories?post=351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/tags?post=351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}