JavaScript EditorFreeware JavaScript Editor     Ajax Tutorials 



Main Page

Previous Page
Next Page

JPSpan

JPSpan is an Ajax framework built with the intention of integrating client-side JavaScript with serverside code written in PHP. PHP has been around since the mid nineties and has grown from simple beginnings to a full-fledged object-oriented language that can run on both Windows and UNIX/Linux platforms. The main advantages of using PHP over other platforms, such as Java or .NET, are that it is smaller, much simpler to install, and more lightweight, needing only a fraction of the memory of the Java runtime or the .NET CLR.

How It Works

JPSpan works by analyzing a PHP class using reflection. It then emits a number of JavaScript methods that accept the same arguments as the methods of the class on the server. When one of these methods is called, it uses a cross-browser library to instantiate the appropriate XmlHttpRequest for the platform it is running on and posts the data to the server. The response is then read and passed to a function on the client for further use within the page.

Note

Reflection in this case means examining a class to find out its properties and methods. It is a commonly available technique in object-oriented languages and is often used as a way of writing generic code that can work with a large number of classes.

Installing JPSpan

The first thing you need is a web server that supports PHP, version 5. This can be the ubiquitous Apache web server for UNIX/Linux machines or IIS for Windows (although others also support PHP, these are the most common). When installing with IIS there is a choice of running the PHP processor as an out of process server using CGI or in process using ISAPI. The first method is recommended for reliability, the second for performance. For the purposes of this chapter, the ISAPI installation was chosen.

Note

You can find the PHP installation, along with a list of compatible web servers, at http://uk.php.net. There is a comprehensive set of instructions with a list of Frequently Asked Questions detailing how to resolve some common difficulties and problems.

The actual JPSpan installation can be found at http://sourceforge.net/projects/jpspan. Aside from the documentation and some examples, it consists of a number of PHP pages alongside several JavaScript files. The entry point, JPSpan.php, sits in a folder named JPSpan, which is best placed directly under the root of your web server (for IIS this is typically C:\InetPub\wwwroot and for Apache it is /usr/local/apache/htdocs). There is a subfolder, also called JPSpan, that contains additional PHP and JavaScript files.

Creating the Server-Side Page

There are two stages involved in developing the server-side page: implementing the business classes that will do the actual processing and hooking them into the framework so that they become accessible from the client.

The first stage is to create a standard server-side skeleton page that can be used as a starting point for most JPSpan projects. This consists of the basic plumbing needed to inform JPSpan of the business class as well as other options such as what to do in the event of an error.

The second stage is the coding of the business class or classes to be used, which requires a knowledge of PHP and the syntax used to declare classes. The syntax is very similar to C++ and Java, so any knowledge of those two languages will help.

After the business class has been tested outside of the JPSpan framework, it can be incorporated into the skeleton page ready for use by the client.

The Standard Page Code

The basic structure of a server-side processing page looks something like the following example:

<?php

//Business class to be included here
class Customer
{
  //Customer class implementation to be included here
}


// Including this sets up the JPSPAN constant
require_once '../JPSpan/JPSpan.php';

// Load the PostOffice server
require_once JPSPAN . 'Server/PostOffice.php';

// Create the PostOffice server
$PostOffice = & new JPSpan_Server_PostOffice();

// Register the Customer class with it...
$PostOffice->addHandler(new Customer());

// This allows the JavaScript to be seen by
// just adding ?client to the end of the

// server's URL

if (isset($_SERVER['QUERY_STRING']) && strcasecmp($_SERVER['QUERY_STRING'],
'client') == 0)
{

  // Compress the output Javascript feature (e.g. strip whitespace)
  // turn this off it has performance problems
  define('JPSPAN_INCLUDE_COMPRESS', false);

  // Display the Javascript client
  $PostOffice->displayClient();

}
else
{
  // This is where the real serving happens...
  // Include error handler
  // PHP errors, warnings and notices serialized to JS
  require_once JPSPAN . 'ErrorHandler.php';

  // Start serving requests...
  $PostOffice->serve();
}

?>

Following the opening PHP tag is the place where the code for the actual class will be included. This can be actually in the page or can be held in a separate file and accessed through one of the PHP include mechanisms.

<?php
//Business class to be included here
class Customer
{
  //Customer class implementation to be included here
}


Previous Page
Next Page




JavaScript EditorAjax Editor     Ajax Validator


©