Basic GWT / PHP Communication (Part 1: Java)

From watching the developer forums and seeing what information is available, it seemed like there was not much for those of us who are PHP developers trying to leverage the power of Google’s Web Toolkit. So after some bumps and starts, here is a bare bones, functional (if not pretty) introduction. I’ll write this as a series, including sample code so you can see it in action.

This is the basic code which I have been using to allow my GWT client to communicate with my PHP server. I wanted something very light-weight that was functional, but not so functional that I couldn’t replace it as soon as better tools come available within GWT. It is my assumption that the challenge in tying PHP with GWT is just a beta feature, and that as development progresses, significantly better support will exist within Google Web Toolkit.

I’m still learning Java, so I seem to have picked an awkward way to provide some of the functionality I’m used to from PHP’s arrays. HashMap would be better than Vector, iterators would be good, and data encoding/decoding would be very nice. BUT, this does work in my application.

There are two components involved here: a Java class and a PHP script.

The Java class defines a simple interface and allows widgets to send/receive data from the server. One method in the interface is getTitle. This is used to send an identifiable name to the server and to properly return data to the requester. Data from the server is passed back to the requester as a String array. When waiting for a server response, this class buffers additional requests, which will be sent once the initial request finishes.

Since HTTPRequest is a static class, I’ve implemented this as a static class as well. To use it, make calls as:
ServerComm.requestUpdate( w);
or
ServerComm.requestUpdate( w, data);
where w is an object implementing the ServerComm.ServerData interface and where ‘data’ is a string containing data to send to the server. Unless encoding is added, the string should not contain anything aside from alphanumerics, ‘-’, ‘_’, or ‘.’. Spaces should be converted to ‘+’. I didn’t add this because I didn’t need it and I figure a GWT update will be available soon to make this unnecessary duplication.

Data is sent to the server in three arrays: sender, data and refresh. Sender and data get values from requestUpdate with a string argument. refresh is always used. refresh and sender contain values from getTitle in the ServerData interface, used to uniquely identify calling objects. sender and data will not be set if there is no data posted to the server.

The expected data format returned from the server is:
title,data1:&:data2:&:data3\n
title2,val1:&:val2

‘\n’ is used to delimit data for separate requesters. The title should appear at the start of the line and be followed by a comma. Data values are then delimited by ‘:&:’. The results are then returned as String arrays to the object with a matching title.

Next up is a description of the PHP side of my implementation.

The Java class is here.