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(){
     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>" + + "<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

This site uses Akismet to reduce spam. Learn how your comment data is processed.