Saturday, September 12, 2009

Why GWT?

Whenever you are studying any framework/technology learn it by WWH way. What is the technology? Why to use this? and how it works?

Lets start with what is GWT?
Google Web Toolkit is open source framework that allow you to write an Ajax based application without a pain. So you can develop Ajax application in Java language. GWT comes with GWT compile that convert Java code into browser compliant HTML and javascript.

Ok so gwt just convert Java code into HTML and JS but why to use GWT?
Writing a code in JS is tedious job from my point of view.
As GWT allow user to write application in Java. GWT comes with java features. OO is the most important. So you can code your ajax application as OO, can reuse code efficiently.
You can write test cases for the GWt app.
It provide real time client side debugging, with the hosted mode browser provided by google or in browser mode.
Also you can use internationalization(i18n) feature in your app easily.
GWT comes with the features like ResouceBundle, ImageBundle. Cacheing.
You can use JSNI(Java Script Native Interface), you can write JS code in Java file.
GWT support most of the standard browsers, means GWT compiler create a compatible JS for supported browsers, also GWT compress the JS code while generating to improve performance.

GWT how it works?
Creating GWT application:
In my post I am going to explain GWT project creation with the eclipse and GWT plugin.

Open eclipse, if you have GWT plugin. Go to file menu -> new -> Web application.
It will show you a dialog box asking 'Project Name' and 'package'. Enter the project name 'MyGwtPro' and package name 'com.google.mygwtpro', plugin will create a GWT project for you.

You will get structure of GWT(1.6) project as follow:
src
+com.google.gwtpro
+com.google.gwtpro.client
+com.google.gwtpro.server
war
+WEB-INF
+lib
+web.xml
+MyGwtPro.css
+MyGwtPro.html

In above packages client package contains java source code which will melt down to JS. In your client package you will get GreetingService.java and GreetingServiceAsync.java, which are useful in Async callback using RPC(Remote Procedure Call). And server side contain all server implementation stuff/servlet stuff.

When you compile code, it will create a folder 'mygwtpro' in your war folder. Where you will find a boot start JS for your application: mygwtpro.nocache.js.

You can run your web app in hosted mode, Go to package explorer right click on your project go to run as and click on Web Application. Same way you can debug your application add breakpoint use debug as.

ImageBundle: Typically, an application uses many small images for icons. An HTTP request has to be sent to the server for each of these images, and in some cases, the size of the image is smaller than the HTTP response header that is sent back with the image data. These round trips to the server for small pieces of data are wasteful. Even when the images have been cached by the client, a 304 ("Not Modified") request is still sent to check and see if the image has changed. Since images change infrequently, these freshness checks are also wasteful.
To solve this problem, GWT introduces the concept of an image bundle. Users can define an image bundle that contains the images used by their application, and GWT will automatically create the composite image and provide an implementation of the interface for accessing each individual image. Instead of a round trip to the server for each image, only one round trip to the server for the composite image is needed. Since the filename of the composite image is based on a hash of the file's contents, the filename will change only if the composite image is changed. This means that it is safe for clients to cache the composite image permanently, which avoids the unnecessary freshness checks for unchanged images.

Other things I am going to explain in my coming posts, things how it works. I will write separate post for above mentioned features. So stay tuned. And enjoy coding with GWT with ease :-)

No comments:

Post a Comment