Tuesday, November 1st, 2005

jsquery: Ajax Javascript result set, http client and server code generator

Category: Java, JavaScript, Library

<p>jsquery is a bridge project, written by David Bennett, that allows you to write JavaScript code on your HTML front end, that will marshall over to a Java server backend to get access to data.

This can be done in an asynchronous manner.

Overview

jsquery is divided into three parts:

  1. Javascript http client

    {WebRoot}/js/jsquery_httpclient.js – A javascript http client that calls a server side query and evaluates the results into a jsquery_resultset

  2. Java server side generator in src/com/bensoft/jsquery

    JSGenerator.java – Generates a javascript representation of a 2D data matrix into a jsquery_resultset instance

    ResultSet2JS.java – Converts a Java JDBC ResultSet into javascript using JSGenerator

    Types.java – Defines the simplified types for jsquery_resultset.js which include Number, String, Date, and Boolean.

  3. Javascript ResultSet

    {WebRoot}/js/jsquery_resultset.js – A javascript implementation of the JDBC ResultSet that reads from aquery result embedded into a series of javascript arrays that are generated by the server.

Test of jsquery HTTPClient

// Make a request for data
var hc = new jsqueryHTTPClient();
hc.setResultSet("rs");
hc.setURL("data.js");
hc.setComplete(new Function("show_results(this.getResult())"));
hc.send();

// Show the results
function show_results(scr){
  // evalulate the result from the server
  eval(scr);

  // generate an HTML table into String
  var html=new String("");
  html=html.concat("");
  html=html.concat("");
  // Example JDBC calls
  var name=rs.findColumn("name");
  var phone=rs.findColumn("phone");
  rs.beforeFirst();
  while (rs.next()) {
    html=html.concat("");
    html=html.concat("");
  }
  // Finish the table
  html=html.concat("
NamePhone
"+rs.getString(name)+""+rs.getString(phone)+"
"); // Find the layer to place the results in if (document.getElementById) { x=document.getElementById("results"); x.innerHTML=''; x.innerHTML=html; } else { // old style document.open(); document.write(html); document.close(); } }

jsquery

Related Content:

Posted by Dion Almaer at 9:09 am
5 Comments

+++--
3.6 rating from 9 votes

5 Comments »

Comments feed

Is this really a good idea? I blogged about it here as it got too long for a comment:

http://www.robsanheim.com/2005/11/01/calling-your-data-layer-via-ajax-jsquery-a-bad-idea/

Comment by Rob — November 1, 2005

Agree with Rob…

This is an unintuitive solution, to embed recieved data one needs to loop through the data with JS which results in unnecessary javascript code…

/hbi

Comment by Hakan Bilgin — November 1, 2005

Always good to hear alternative opinions chime in.

For the record:

jsquery does not circumvent a good MVC model in anyway. The ResultSet can be completely managed by the controller which is what the example code demonstrates. The ResultSet is merely a well defined 2D data delivery pattern that is universally understood. jsquery leverages existing API’s which is a good thing and dramatically shortens the learning curve.

In production we use jsquery with an OR Mapper for the model and a full controller using POJO (I’m not big on overblown architectures that add bloated unnecessary layers of code to an application).

The other thing to note:

jsquery is VERY FAST and VERY LIGHTWEIGHT

Much faster than a great deal of the piggy XML based solutions that are out there.

jsquery allows you to add AJAX functionality to your web applications without learning a new API and adopting gigantic new proprietary paradigms.

As for the ‘unnecessary javascript code’ please refer to the examples. The client side code is very tight and small, much smaller than other AJAX implementations.

Comment by David Bennett — November 2, 2005

The jsquery architecture is not really any different then most MVC frameworks out there.

Current Frameworks (Struts, WebWork, etc..)

1. Execute query on db
2. Write results into Java Object
3. JSP renders results from the Java Object, or loops through a list of objects depending on what type of page it is.

JSQuery

1. Execute query on db.
2. Write results into JS Array.
3. View can access the array (through JDBC like methods) and displays information to the page.

As Dave said, the JS is being directly returned to the view from the server request. It is NOT getting back xml that has to be traversed to build the array. This makes jsquery very fast.

The fact that data can be retrieved without having to do a page refresh will greatly increase the speed of an app. Not to mention stop most of the need to have parameters being passed everywhere to maintain the current page state in-between refreshes.

IMHO, more and more people are going to be gravitating towards httprequest/ AJAX. Especially with the success Google is having with it, and MS is now making a push towards it as well.

My 2 cents….

Paul

Comment by Paul Zepernick — November 5, 2005

Yeah, AJAX…

Comment by fgsdgsg — August 19, 2007

Leave a comment

You must be logged in to post a comment.