AJAX Javascript Resultset, http client and server code generator.
Version: 1.0 - Copyright 2005, Bensoft Inc.
Summary
An implementation of an AJAX javascript data generation server, http client, and client result set
Uses a javascript http client to dynamically map the results of data requests from a Java application server to a web browser HTML form without requiring refresh or page submit (similar to Google Suggest). Server side data can be SQL via JDBC, an object
relational mapping using a tool such as Hibernate, returned from a server connecting a group of peers or pipelined from another source like a SOAP server.
In other words, jsquery allows the pages in your web application to dynamically get new information from the a server application without requiring a page refresh. For a quick functional example of why you would want to do this, see
the
sql zip code example
.
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.
Javascript ResultSet
{WebRoot}/js/jsquery_resultset.js
- A javascript implementation of the JDBC ResultSet that reads from a query result embedded into a series of javascript arrays that are generated by the server.
getzip.jsp
- Server side zip result provider, uses JDBC auto-mapping.
Using your favorite datasource with jsquery
In a nutshell, your data translator should call
JSGenerator
methods in the following order:
Send the metadata (data definition):
writeResultSet - client side declaration of jsqueryResultSet object.
writeTableName - set the name of the return table
addColumnDef - LOOP: add a column definition (call for all columns).
writeColumnNames - write the column names Array to the client javascript.
writeColumnTypes - write the column types Array to the client javascript.
Or use the quick metadata method:
addColumnDef - LOOP: for each column defined
writeMetaData - in place of all writeXXX methods above
Then iterate through your 2D data:
LOOP: for each row of data
addColumnValue - LOOP: for each column definition above
writeRow - at end of columns
when done call:
writeEnding = Stub for stuff to do afterwards.
How it works - Step by step:
A javascript client function uses the http client to make a request from the server. The request can be encapsulated into a JSP or a Servlet. This is done without any page refresh or submission. A function is defined to call when the data is ready, and
the javascript function returns immediately.
The JSP/Servlet reads the request parameters from the URL (GET refs) and generates data. The source can be anything the server has access to such as a SQL database, Object Relational Map, SOAP service, etc...
The JSP/Servlet then returns the data as a set of names and types (metadata) and a 2-dimensional data grid (rows of columns) using javascript Array objects. This data is encapsulated into a block of javascript (1.3. spec) so that the client can easily
parse the result using the eval() function. The server side 'streams' the resulting javascript to minimize server memory load.
When the transfer is complete, the http client automatically runs a user defined javascript function that evaluates the result from the server and processes it using a JDBC ResultSet style interface. The client receives the entire result set at once
requiring enough memory on the client to hold the entire data set (in other words, don't send 10,000 rows of a large data set to the client!)
TODO
Server Side Application Layer Query Security Layer. I am thinking about using a library like
A JS/DHTML 'history' dropdown could be incorporated to provide a server-side suggestion interface.
Add user interface component to dynamically populate and set SELECT/OPTION elements.
I decided not to tackle sending the data to the server via HTTP POST. Looks like it's not supported well by some browsers and more trouble that it's worth. Let me know if this is a problem for you and I'll look into supporting it.
A chat application using zero-disk and an application context would be cool.
An app context multi-user game of some sort.
History:
I first experimented with the idea of generated code for data encapsulation back in the mid 90's when I was writing
perl
based content systems for the
web
with
Data::Dumper.pm
and
eval()
Recently, I noticed several organizations using the
XMLHttpRequest
API in combination with the same techniques. evaluating server prepared
javascript
code on the
browser
client.
I needed this for various projects so I wrote jsquery as a
Javascript
<.>
Java
implementation and documented it to help explain the concept to others.
Advantages of this script encapsulation technique:
Easy to develop, you only need to write one side of the data protocol.
Fast! using an
eval()
type function is usually much faster then an interpreted script-based parser.
Lower memory footprint. Lightweight components can be used with native data types generated by the protocol side you did write :)
If you wish to include jsquery in your commercial product, or if you require a different licensing arrangement, please contact us at
sales@bensoft.com
.
Special Thanks to:
Paul Zepernick - Fixing bugs in javascript resultset and example getzip.jsp