A first glance at GWT

JavaScript March 9th, 2009 @ 15:03

Web applications of today are often required to be quite complex – AJAX offers lots of nifty ways of giving the user a great experience. But developing stable applications using lots of JavaScript code can be quite challenging. Even though there’s help from great libraries such as Prototype, YUI and jQuery, it’s tricky to keep the code bullet-proof and fully tested.
Also, it can be difficult maintaining a decent architecture as the application grows, i.e. how to implement an MVC solution.

There’s of course a few people out there trying to address these issue, one of which being Google Web Toolkit (GWT).
We’ve discussed GWT from time to time, but haven’t really made a true effort of delving into it. I decided to try to find out how it really works and if it’s something for us to use in our applications.

Do we really need it?

I guess the first question you need to ask yourself is if you really need anything like GWT. Is our goal to build a full-feathered AJAX site without any browser reloading of pages? Do we want something with features like Gmail (from dialogues to rich-text editors)?

After researching GWT for a while, my feeling is that you either fully implement it (with effects on server-side and all) or you stick to using “old, self-developed” Javascript code. If you’re to have any kind of client-server communication (which you are), you’ll probably have to make some adjustments on the server-side as well.

Anyway… I tested a sample implementation usin GWT and as a Java fan, I was of course enjoying writing nice, strict Java code with full IDE support instead of regular JavaScript. There was really nothing to complain about when it comes to writing GWT code and it was fairly easy to get started and the compilation was quick enough and provided a nice debugging tool.

Are there any limitations?

My main concern is that using GWT will come with some limitations:

  • What if I don’t what to write the code exactly like it’s done with GWT? Will I have to write all my code using GWT?
  • How painful is it really to mix “regular” Javascript with GWT code (they offer a way of doing it, but it doesn’t seem like something you’d really want to do)?
  • Is it really stable enough for an entire application/game? To be honest, the GWT site itself is actually quite buggy…

One nice feature that comes with GWT is the support for creating modules. It seems quite easy to create a smaller module (perhaps a text-editor) and plug it in to your application. I’d say that doing so would be a first good test of GWT, as it still feels a bit scary to rely on it for an entire application (i.e. creating entire pages, browser history states etc).
Maybe using it for smaller modules the best use of GWT in our case? It would be really nice if server-developers (Java guys) could implement advanced client-side features without being afraid to mess it all up… :-)

To sum it up…

I guess these questions never will be answered unless I/we actually try it out in a real implementation (or at least a real-enough prototype). There’s obviously a whole lot of things with GWT that is really nice, but I wouldn’t feel fully comfortable relying on it as of today. Trying it out in a module or two would probably be the best way to draw some real conclusions.

In the end, it’s all about what you and your product really need.

References

Google Web Toolkit Website

GWT in Practice – Ebook (lots of reading)

Ext GWT Library (great widgets)

Unofficial GWT blog

Changing page without reloading browser

JavaScript March 6th, 2009 @ 13:03

I wanted to investigate if we can build web applications that doesn’t require reloading of pages in the browser, according to the principles of  Life above the Service Tier.

Evaluation

I started by reading the “Life Above the Service Tier” document, which was a description how it works today (and its flaws) and what the proposed solution is.
Also, I browsed the web for more articles, blog posts and comments on the issue.

Summary

The ideology (called SOFEA: Service-Oriented Front-End Architecture) is basically to put more rendering logic on the client side and only use the server side to transport data back and forth.

The principles of SOFEA could be implemented with help from AJAX. The controller resides on the client-side and is responsible for loading (XML) data and rendering the application using the received data, without actually reloading the page in the browser.
This means that JavaScript would be used instead of HTML/JSP to render the server data. Also, the server-side is much thinner and not really in need of any web framework (as all it does is receive messages and return XML data).

Conclusion

The principles of SOFEA seem very far away from today. There are several flaws with implementing in the suggested manor:

  • The web clients (browsers) of today are not good enough to handle could have problems handling as much logic as a large web site would require (given that old IE versions have so many users, ca 70-85%)
  • The client code (JavaScript) and architecture is not really mature/good enough to handle this much logic, given that it needs to be bullet-proof, easy to test etc (EDIT: unless using GWT or similar – it’s at least difficult to manage)
  • Developing applications would take much more time (hence be more expensive) as more manual labour is required to render content using JavaScript instead of letting the JSP engine do it with JSP/HTML/PHP/etc

Everything points to NOT implementing it in such a manor. At least not at this time, partly because the browser/client market isn’t ready for it.

Is there an other way?

Yes, there are other solutions to the main issue; changing pages without actually reloading the container (browser).

One suggested solution could be an extension to our model in Taikai, where AJAX can be used in combination with both XML, HTML/JSP and JSON, hence giving us the possibility to smoothly serve a full JSP page using AJAX.

A brief description of how it could be solved:

  • Make a JavaScript “controller” handle all URL requests to the server, in order to bind the answer to a certain action.
  • Set up “View XML files” for all views that are to be loaded with AJAX. The view could look something like this:
<?xml version="1.0" encoding="UTF-8"?>
<xmlRoot>
<title>Document title</title>
<styles>
<src>/css/foo.css</src>
<src>/css/bar.css</src>
</styles>
<scripts>
<src>/js/foo.js</src>
<src>/js/bar.js</src>
</scripts>
<content id="targetContainerId">
<![CDATA[<jsp:include file="foo.jsp" />]]>
</content>
</xmlRoot>
  • When returning the response from the server, the returning page would be the pre-defined XML (as per above).
  • The controller parses the contents of the returned XML file and performs its magic:
  1. Changing the document title
  2. Loading new styles and scripts (and possibly unloading old ones to keep the page slim)
  3. Updating the content element (with id=”targetContainerId”) with the provided content, which has been rendered by the JSP engine and therefore taking advantage of its power (i.e. JSTL, HTML tags etc etc).
  4. Updating the browser history using hash (#) technique, allowing for back/forward movement while remaining in the same page container

The main advantages with such a solution would be:

  • Allowing developers to create content using known and powerful formats, such as HTML and JSP
  1. Very important in respect to time efficiency
  2. Still allows for easier debugging and development
  3. No need to put too much logic on the JavaScript side, i.e. creating elements etc (although it could still be done in some components, if needed/wanted)
  • Still providing the user with the quicker experience of a site without (or with few) full page reloads

Remaining questions:

  • Would there be any risk of memory leaks, especially in older Internet Explorer browsers? And is there an easy way of eliminating these?
  • Is the support for “hash history technique” good enough in all major browsers?

References