Skip to main content

What is GWT ? Google Web Toolkit

The GWT project states that,

GWT is a development toolkit for building and optimizing complex browser-based applications. Its goal is to enable productive development of high-performance web applications without the developer having to be an expert in browser quirks, XMLHttpRequest, and JavaScript. GWT is used by many products at Google, including AdWords, AdSense, Flights, Hotel Finder, Offers, Wallet, Blogger. It’s open source, completely free, and used by thousands of developers around the world.

I quote the development lifecycle of GWT App from the project site as well.

Write : The GWT SDK provides a set of core Java APIs and Widgets. These allow you to write AJAX applications in Java and then compile the source to highly optimized JavaScript that runs across all browsers, including mobile browsers for Android and the iPhone.

Constructing AJAX applications in this manner is more productive thanks to a higher level of abstraction on top of common concepts like DOM manipulation and XHR communication.

You aren’t limited to pre-canned widgets either. Anything you can do with the browser’s DOM and JavaScript can be done in GWT, including interacting with hand-written JavaScript.

Debug : You can debug AJAX applications in your favorite IDE just like you would a desktop application, and in your favorite browser just like you would if you were coding JavaScript. The GWT developer plugin spans the gap between Java bytecode in the debugger and the browser’s JavaScript.

Thanks to the GWT developer plugin, there’s no compiling of code to JavaScript to view it in the browser. You can use the same edit-refresh-view cycle you’re used to with JavaScript, while at the same time inspect variables, set breakpoints, and utilize all the other debugger tools available to you with Java. And because GWT’s development mode is now in the browser itself, you can use tools like Firebug and Inspector as you code in Java.

Why do you need the browser GWT plugin if all GWT does is to translate java into js?
Ah. It's just for the development cycle. Optimizing large programs to js consumes valuable development time and GWT plugins in the browser can communicate with the GWT plugin in eclipse IDE to do some magic to help the developers out cut development time.

Optimize : GWT contains two powerful tools for creating optimized web applications. The GWT compiler performs comprehensive optimizations across your codebase — in-lining methods, removing dead code, optimizing strings, and more. By setting split-points in the code, it can also segment your download into multiple JavaScript fragments, splitting up large applications for faster startup time.

Performance bottlenecks aren’t limited to JavaScript. Browser layout and CSS often behave in strange ways that are hard to diagnose. Speed Tracer is a new Chrome Extension in GWT that enables you to diagnose performance problems in the browser.

Run : When you’re ready to deploy, GWT compiles your Java source code into optimized, stand-alone JavaScript files that automatically run on all major browsers, as well as mobile browsers for Android and the iPhone.

OK. I played around with the StockWatcher application in the GWT tutorial. 
It has the following structure.


It has 3 packages for the client side, server side and shared code.

Note: jetty is the servlet container in GWT plugin for eclipse

Client Side Programming

 In the client side you design the view of the GWT app using widgets.
You have many pre designed widgets panels, tables, test areas, trees, buttons, labels and everything that you place in the UI.
Then you can add event handlers to these widgets.
AJAX runs under the hood to fetch you data from the server to make changes to your UI.

All these gets converted to js and embedded in the GWT apps html file.
You can also add HTML content to the file.
The css can be changed to different themes to get your desired look and feel on the GWT application.

Server Side Programming

Everything that happens within your web server is referred to as server-side processing. When your application running in the user’s browser needs to interact with your server (for example, to load or save data), it makes an HTTP request across the network using a remote procedure call (RPC). While processing an RPC, your server is executing server-side code.

GWT provides an RPC mechanism based on Java Servlets to provide access to server-side resources. This mechanism includes generation of efficient client-side and server-side code to serialize objects across the network using deferred binding.

Tip: Although GWT translates Java into JavaScript for client-side code, GWT does not meddle with your ability to run Java bytecode on your server whatsoever. Server-side code doesn’t need to be translatable, so you’re free to use any Java library you find useful.

GWT does not limit you to this one RPC mechanism or server-side development environment. You are free to integrate with other RPC mechanisms, such as JSON using the GWT supplied RequestBuilder class, JSNI methods or a third party library.

Remote Procedure Calls

A fundamental difference between AJAX applications and traditional HTML web applications is that AJAX applications do not need to fetch new HTML pages while they execute. Because AJAX pages actually run more like applications within the browser, there is no need to request new HTML from the server to make user interface updates. However, like all client/server applications, AJAX applications usually do need to fetch data from the server as they execute. The mechanism for interacting with a server across a network is called making a remote procedure call (RPC), also sometimes referred to as a server call. GWT RPC makes it easy for the client and server to pass Java objects back and forth over HTTP. When used properly, RPCs give you the opportunity to move all of your UI logic to the client, resulting in greatly improved performance, reduced bandwidth, reduced web server load, and a pleasantly fluid user experience.

The server side that gets invoked from the client is often referred to as a service, so the act of making a remote procedure call is sometimes referred to as invoking a service. To be clear, though, the term service in this context is not the same as the more general “web service” concept. In particular, GWT services are not related to the Simple Object Access Protocol (SOAP).

Note: A web service is a remote server hosting an application that provides a particular service. For example amazon provides cloud services using their web services. Clients can connect to these web services by communicating via different protocols. SOAP and RESTful APIs are such communication protocols. This is required to ensure compliance across different users of the web service.
The first to the scene was SOAP developed by Microsoft. SOAP used XML to encode and decode requests and responses. However it proved hard to use because of the enforcement of proper XML every time. This however bought the advantage of inbuilt error response because if a wrong XML message was sent to the service, the users got replies back pointing to the error in XML.
The major problem was its heavy weight - security measures, reliability, atomicity etc. requiring lots of code on the client side, e.g. js.
REST was the newcomer which was much lightweight. It doesn't parse XML and some people consider RESTful APIs as just an architectural style not a protocol. Unlike SOAP, REST doesn’t have to use XML to provide the response. You can find REST-based Web services that output the data in Command Separated Value (CSV), JavaScript Object Notation (JSON) and Really Simple Syndication (RSS).
If the server responses are pretty simple, you really don't need such fancy protocols, just simple HTTP with text protocol would suffice, or even parametrized HTTP responses would do. It's all because of the data the web services send !!!

A quick look at how to get GWT RPC calls to work.



Here's some sample code which describes how the synchronous interfaces are need to be set up and how the asynchronous interface is set up with a callback.


The nature of asynchronous method calls requires the caller to pass in a callback object that can be notified when an asynchronous call completes, since by definition the caller cannot be blocked until the call completes. For the same reason, asynchronous methods do not have return types; they generally return void. Should you wish to have more control over the state of a pending request, return Request instead. After an asynchronous call is made, all communication back to the caller is via the passed-in callback object.

Retrieving JSON Data via HTTP

What if the server has no support for java?
If your application talks to a server that cannot host Java servlets, or one that already uses another data format like JSON or XML, you can make HTTP requests to retrieve the data. GWT provides generic HTTP classes that you can use to build the request, and JSON and XML client classes that you can use to process the response. You can also use overlay types to convert JavaScript objects into Java objects that you can interact with in your IDE while developing.

Comments

Popular posts from this blog

Detaching a process from terminal - exec(), system(), setsid() and nohup

Linux processes are created by fork() and exec(). The very first process of POSIX systems is init and subsequent processes are derived from the init as parent. These subsequent processes are child processes. During forking the parent process copies itself - with all I/O, address space, stack, everything. The only thing that is different is the process ID. The parent and child will have 2 different process IDs. The system() library function uses fork(2) to create a child process that executes the shell command specified in command using execl(3) as follows: execl("/bin/sh", "sh", "-c", command, (char *) 0); system() returns after the command has been completed. system() executes a command specified in command by calling /bin/sh -c command , and returns after the command has been completed. During execution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT will be ignored.  system() calls are often made within programs to execut

Vaadin vs GWT

From Chapter 1 of book of Vaadin I quote the following. Vaadin Framework is a Java web application development framework that is designed to make creation and maintenance of high quality web-based user interfaces easy. Vaadin supports two different programming models: server-side and client-side . The server-driven programming model is the more powerful one . It lets you forget the web and program user interfaces much like you would program a desktop application with conventional Java toolkits such as AWT, Swing, or SWT. But easier. While traditional web programming is a fun way to spend your time learning new web technologies, you probably want to be productive and concentrate on the application logic. The server-side Vaadin framework takes care of managing the user interface in the browser and the AJAX communications between the browser and the server . With the Vaadin approach, you do not need to learn and deal directly with browser technologies, such as HTML or JavaScript.

C++ Callbacks using function pointers vs boost bind +boost function

In C, the most common uses of callbacks are as parameters to library functions like qsort , and as callbacks for Windows functions, etc. For example you might have a library that provides some sorting functions but you want to allow the library user to provide his own sorting function. Since the arguments and the return values do not change depending on the sorting algorithm, this can be facilitated in a convenient manner using function callbacks. Callbacks are also used as event listeners. onMouseClick(), onTerminalInput(), onData(), onConnectionStatus(), onRead() are probably some examples you've already seen in different libraries. The libraries have these callback functions and the users of the library are supposed to implement them. The library has a function pointer to these functions and calls them on their event loop which will invoke the code of the inherited classes of the library user. The implementation of function pointers is simple: they are just "code p