Home Overview Demos/Webs News Download/Install Docs/Support Investors/Partners Commercial Tutorial: Simple ItsNat (Core) Application Tutorial: Simple ItsNat (Core) STATELESS Application Tutorial: Single Page Interface SEO Compatible Web Site With ItsNat STATEFUL Using HASHBANGS Tutorial: Single Page Interface SEO Compatible Web Site With ItsNat STATELESS Using HASHBANGS Tutorial: Single Page Interface SEO Compatible Web Site With ItsNat STATEFUL Using HISTORY API Tutorial: Single Page Interface SEO Compatible Web Site With ItsNat STATELESS Using HISTORY API Tutorial: Hybrid Client-Server Centric Programming
Tutorial: Hybrid Client-Server Centric Programming

Last update: 2011, Mar. 11  


Reconciling client-server centric approaches with ItsNat

First of all we must need to define what client-centric and server-centric programming in the web is.

Client-Centric Programming

You do client-centric programming when you make code to be executed on the client. For instance, any JavaScript code made by hand by you is client-centric programming (regardless of whether or not you use a JavaScript library). The more custom JavaScript the more client-centric is your application.

Even if you code in Java with GWT, this technology is client-centric because your code will be executed in client.

Server-Centric Programming

You do server-centric programming when your code is going to be executed on the server, of course your code will generate HTML and may generate JavaScript under the hood.

Client and server centric mismatch

Both approaches have played together for years in conventional paged web sites, the server generates the page containing custom JavaScript and this custom JavaScript has provided us some kind of dynamic behavior. With the advent of AJAX even more dynamic behavior has been added to our web sites.

Notwithstanding everything has changed with Single Page Interface, in this kind of AJAX and JavaScript intensive sites/applications, web frameworks, client and server centric, tend to be too invasive with tons of already made black-boxed components. That is, there is not much room to coexist and cooperate, so you are usually compelled to select one approach and one framework. For instance you hardly can add custom JavaScript code to add some value added to server-centric components. The opposite is also true, some JavaScript based components are too closed to be extended with some kind of server-centric processing.

Another problem is many already made JavaScript components are designed only for page based applications. There is not very much support for Single Page Interface, because in this paradigm page fragments are replaced on demand usually loading new components.

Reconciling client-server centric approaches with ItsNat

In spite of its server-centric nature, ItsNat is highly flexible. With some care you can make web applications with zones controlled by ItsNat and zones controlled by custom JavaScript code. Custom JavaScript code can be returned as the result of AJAX requests, and attributes of any element can be changed by custom JavaScript code, for instance to provide some kind of visual effect (usually onX handlers, style and class attributes), in this case the client-server synchronization is broken (attributes modified only in client) but this is not a problem in ItsNat.

As of ItsNat v1.1 a new feature was introduced, disconnected nodes from client, server DOM nodes can be removed only in server remaining client DOM nodes intact. Later the same DOM subtree in server can be reconnected again in sync with client.

This feature is very interesting to bring a new world of cooperative client-server development. The following example shows how a typical black-boxed JavaScript component can be fully managed from server in a Single Page Interface application.

JCarousel is a beautiful jQuery based component to provide carousel motion to a list of images. Online example.

JCarousel is a "semi" black-boxed component, because some templating is possible, the list of the images is defined by us with something like:

      <ul id="carouselId" class="jcarousel-skin-tango">

In some location of the page, this script must be executed to invoke JCarousel to render the carousel when the page is loaded:

The jCarousel layout seems to be very clean based on plain HTML, this is not true, the initial layout is just to provide the list of images, this HTML code will be fully replaced (rendered).

ItsNat just can provide the required initial layout of the jCarousel component, because the final DOM rendered by jCarousel on the client is very different to the initial DOM. This final DOM is no longer in sync with the server, therefore the DOM in server used to set up jCarousel must be ignored otherwise we can break our ItsNat application. Yes we can integrate jCarousel but once rendered we cannot change the component for instance adding new images with ItsNat… unless we rebuild the component again.

The following example shows how we can use jCarousel as a client based extension of ItsNat in client but fully managed in server.

We are going to create an ItsNat based web application.

Create an empty web application with your preferred IDE, the context name is not relevant, in this example we will use inexperiments.

Download ItsNat and follow the instructions of "What does a new ItsNat based web application need?"

Download jCarousel and decompress (v0.2.7 is the version used in this tutorial).

Create a public web folder with name hybridcs and copy the folder jsor-jcarousel-XXXXXX into, and renamed to "jcarousel". For instance, the file jquery.jcarousel.pack.js must be public as:


The port number may be different.

To avoid remote image loading from Flickr (just for performance), copy the images linked in jCarousel examples:


To the public folder hybridcs/img

Create a new servlet with name inexpservlet (this name is just an example and is not mandatory) with your preferred IDE. Default source code and registration in web.xml is fine.

Replace with the following code:

Add the following pure HTML template (which is registered with name hybridcs):

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<title>Hybrid Client-Server Centric Programming

<link href="hybridcs/jcarousel/style.css" rel="stylesheet" type="text/css" />

<script type="text/javascript" src="hybridcs/jcarousel/lib/jquery-1.4.2.min.js">

<script type="text/javascript" src="hybridcs/jcarousel/lib/jquery.jcarousel.min.js">

<link rel="stylesheet" type="text/css" href="hybridcs/jcarousel/skins/tango/skin.css" />
<body xmlns:itsnat="http://itsnat.org/itsnat" >


Hybrid Client-Server Centric Programming

This example shows how ItsNat can cooperate with client JavaScript libraries like jQuery and black-boxed JS components like jCarousel.

<ul id="carouselId" class="jcarousel-skin-tango"> <li> <li> <li> <li> <li> <li> <li> <li> <li> <li> </ul>

URL of the new Image:

RETURN </body> </html>

to the file WEB-INF/hybridcs/pages/hybridcs.html.

Create the Java file HybridCSLoadListener in package inexp.hybridcs:

The method processRequest will be called whe the page is being loaded.

And the Java file HybridCSDocument in the same package:

As you can see most of the code is just Java W3C DOM HTML and W3C DOM Events.

The key method is ItsNatDocument.disconnectChildNodesFromClient(Node), this method removes the content of the element only on the server, the client remains the same. So jCarousel can render the carousel with no problem.

Because we have saved the initial setup of the carousel, we can restore the removed nodes, just inserting the content again into the parent element of the DOM subtree being disconnected. Before inserting, ItsNat automatically clears the content in client, in this case the jCarousel component is fully removed, now new nodes will be also added to the client and again the client is in sync with server, then we perform the required changes to the initial layout (add a new image in the specified position) and jCarousel is called again to render the new component, in this case a new parameter is provided to scroll the component to the new image.

Fortunately jCarousel detects when a component is no longer in the page with no problem and can render new components beyond page load phase.

Deploy, run and open the example with this URL (port may different).


See it already deployed online in action.


This example has shown how we can fully manage a semi-black-boxed JavaScript component from server in a Single Page Interface environment with minimum custom JavaScript.

Terms of Use Privacy Statement Contributor Agreement Add to deli.cio.us Single Page Interface logo ItsNat logo Innowhere logo