Sunday, March 20, 2011

Getting started with GWT

GWT short for Google Web Development Kit lets programmers to develop Ajax web applications with java. Java codes are converted in to JavaScript and html codes by the GWT compiler. GWT applications are called modules and modules are described using a xml file, assuming the module name as "mymodule" name of the xml file would be "mymodule.gwt.xml". There is atleast one entry point for a module and it is same as the main method in a java program. GWT program code executes with in a HTML file and CSS file is used for alternating the look and feel.

Now that we are familiarized bit with terms and stuff we can move on to writing our own GWT application.

Step 1: Installing GWT plugin for eclipse

If you have already installed eclipse all you have to do is go to "Help" menu, under that "Install New Software" and give the URL as and click next and complete installation.

step 2: Creating a new project

Select File > New > Web Application Project. Enter the name "" for the project name and package. By default it will select both "Use Google Web Toolkit" and "Use Google App Engine" as we only using Google Web Toolkit here you will have to deselect Google App Engine.

Now eclipse will create you a project with a package structure as shown below.

step 3: Creating the entry point

Inside the package package create a java class named "HelloWorldGwt"


public class HelloWorldGwt implements EntryPoint {

 public void onModuleLoad() {
  final Label label = new Label("Hello World GWT !!!");
  final Button button = new Button("Click Here");
  button.addClickHandler(new ClickHandler() {
   public void onClick(ClickEvent event) {
    button.setText("welcome back again!");
    Window.alert("Welcome to GWT");


Inside the package create Com_eviac_blog_helloworld.gwt.xml file.


<?xml version="1.0" encoding="UTF-8"?>
<module rename-to='com_eviac_blog_helloworld'>
  <!-- Inherits Web Toolkit utilities.                        -->
  <inherits name=''/>

  <inherits name=''/>

  <!-- Specify the app entry point class.                         -->
  <entry-point class=''/>

Step 3: Creating html page

Inside the folder war create Com_eviac_blog_helloworld.html file


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <link type="text/css" rel="stylesheet" href="Com_eviac_blog_helloworld.css">
    <title>Hello World GWT</title>
    <script type="text/javascript" language="javascript" src="com_eviac_blog_helloworld/com_eviac_blog_helloworld.nocache.js"></script>


    <!-- optional tag: add this if needs history support -->
    <iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position:absolute;width:0;height:0;border:0"></iframe>
    <h1><center>Hello World GWT</center></h1>

Step 4: creating web.xml file

Inside the folder war/WEB-INF create a xml file called web.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

 <!-- Default page to serve -->

Step 5: Creating css file

Inside war falder create a css file named Com_eviac_blog_helloworld.css


h1 {
  font-size: 2em;
  font-weight: bold;
  color: #6495ED;

.gwt-Label {
 color: #0000FF;
 font: normal 12px tahoma, arial, helvetica, sans-serif;
 width: 10.7em;  

.gwt-Button {
 color: #0000FF;
 width: 10.7em;
 font-size: 12px;
 font-family: arial, sans-serif; 

Alright now we are done with coding steps but wait for it, one more step to go

Step 6: Running GWT Application

To run right click on the project select Run As->Web Application then it will bring up a new view "Development Mode" copy the url generated.

Install GWT plugin for your web browser using this link.

Now paste the url in the browser and you'll see something like following and that's it.

Now that you know how to build a basic GWT application you can improve it by adding more functionalities and changing the look and feel using css file.


  1. Thank you. This is very enlightening.
    All that lines and files involved in a simple "hello world". Mmmmmmmm. I'll probably choose flex for my web apps.

  2. kar9999, just a note : Flex needs a plug-in to run. GWT (and JavaScript) just a browser.

    When Flash to HTML5 will be consolidated ( I may agree with your statement.

    For sure GWT is not simple but really powerful if you are a Java programmer.

    Did you try Spring-Roo ( With few lines of code you can create an "Hello World" application with GWT :)

  3. Good article. We worked a lot with GWT, you can achieve impressive things, but often you have to get mad with little discrepancies between browsers.
    This is what we made with GWT : (NSFW! it's it's a video porn web site, made as a single GWT app, the backend is written in Django/python).

  4. Thanks a lot everyone for the comments :-)