GWT1.7-PHP5.2-JSON

Here I’ll present a “quickstart” application of the GWT + PHP. There are a lot of different approaches to do GWT-PHP communication, I’ll use JSON. In my approach, I’ll use two bean-like (Java) classes PHPRequest and PHPResponse, which encapsulate sending and receiving data. Of course, in so simple example it’s piece of additional job, but while application is growing, this small job effects in clarity and easy maintainance of the code. This application has been build as re-make of “JSON” application from samples of GWT 1.7.

For impatients – live demo and source code.

Here you can see live demo, and here you can download sources.

Requirements.

  • GWT 1.7 ( here )  ( I use 1.7 which is currently the newest one, but it should work on the 1.6 also. Older versions have incompatible compiler, so you’ll need do some tweaks)
  • PHP 5.2 ( since this version json_decode and json_encode methods are build-in, in older you’ll need to use JSON libraries ). On Windows, I use EasyPHP.
  • Java 1.5 or higher, Ant, some IDE ( I use Eclipse Galileo PDT ).

Idea.

I’ll create application based on two projects – app-php and app-gwt. First one will contain files we want to put on the PHP server – after build this folder can  be copied to the public server. Basically, there is JSON.html file ( which we open in order to see application ) and all goodies compiled from GWT ( json folder ). In the server dir, we have json-dispatcher.php which is target of JSON call. The second project, is the GWT source and the ant buildfile.

6-steps flow.

  1. View part contains TextBox where you can put your name (or any swearword you want).
  2. When you click “Search” button, data are being encapsulated inside of the PHPRequest class.
  3. PHPRequest object is being pushed using JSON to the server’s json-dispatcher.php.
  4. Server parse request, and create response of the type PHPResponse  (php) and send back to GWT.
  5. GWT receives response as the callback, and transforms it into PHPResponse (java) object.
  6. PHPResponse object is being send to the method, which displays its content inside the labels.

How to run it – configuration.

  1. Download and extract sources. Put app-gwt to your workspace, and app-php inside www of you PHP server. You can use import->project in Eclipse.
  2. Open build.xml file from app-gwt . And take a look on:
    <property name=”gwt.sdk” location=”D:/dev/gwt-1.7.0/” />
    <property name=”php.app.dir” location=”c:/EasyPHP-3.0/www/app-php/” />
    gwt.sdk is obviously directory of your GWT
    php.app.dir is directory, where compiled GWT files are being copied.
  3. If your server is running, you should be able to see application under http://localhost/app-php/JSON.html
  4. Look through the projects, do changes, play.
    ant gwtc – will compile the project
    ant php
    - will compile AND copy the result inside of the preconfigured directory ( point 2 )
    ant hosted - will not work ( we don’t use Tomcat )
  5. Mark that on the server side ( app-php/server/json-dispatcher.php ) code responsible for creating response is put between “** logic**” comments. If you create more request, you will need to add condition, which response is valid for which request.

Hope this was helpful as good quickstar point !

Tags: , , , , , , ,

Leave a Reply