REST services are a snap with Dojo

As I sit here late at night thinking about what to write about I figured I would share some insight into a night time project I have been playing around with.  The project uses a basic Java servlet on Tomcat that serves up JSON to clients.  I have constructed the servlet to handle many different kinds of requests based on what parameters are passed into it.  The reason I wanted to blog about this was because my first client is being implemented in JavaScript, Dojo and HTML 5.  I am using Eclipse, WTP, and Aptana as the development environment – which I must say is amazingly simple to code, launch, and debug.  The great thing is I can write the Servlet code (in Java), the client wrapper (HTML), and the client-server interaction (JavaScript) all from the same IDE and debugger.  I do however find myself launching in FireFox to use Firebug for some debugging but outside of that its pretty self contained.

I chose the REST service to emit JSON because it makes it very easy to process on the client.  Check out the Dojo code below that creates a session with the “Base” servlet and then processes the response as a regular JS object, in this case “session = dojo.fromJson(response);“:

var session = null;

function createSession(){
 dojo.xhrGet({
     url : "Base?c=cs",

     handleAs : "json",

     load : function(responseObj, ioArgs){
         console.log("successful session creation", responseObj ,ioArgs);
         session = responseObj;
         dojo.byId("session-info").innerHTML = "<b>Id = </b>" + session.id + "<br>";
         return responseObj;
     },
     error : function(responseObj, ioArgs){
         console.log("failed session creation", responseObj ,ioArgs);
         dojo.byId("session-info").innerHTML = responseObj;
         return responseObj;
     }
 });
};

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s