Java, PHP, XML and AJAX (how to mix all together)
Introduction
Why does anyone want to mix all this stuff together? are you crazy? maybe i am ![]()
The main purpouse of this experiment is to test the real availability of this heterogeneus environments working together.
First of all, i want to explain in a nutshell what i’m going to to and how.
I’ll use the powerfull platform of Java to implemente all the application logic. In this test, the application logic will be very simple, but in real scenarios maybe we want to use advanced features like replication, clustering, relational databases, work with XMLs documents, operations like XSL-T, XPath, XQuery, and so on. And there isn’t a better solution than Java.
With PHP you can code big projects, but it doen’t have all the Java third party APIs and the big comunity of the open source. But always is interesting to use the strengths of each other, and the web services is a way to connect both.
In Java i’m going to use Apache Axis to expose a web service and in PHP i’m going to use NuSOAP API to connect to this web service.
And the last step is to use AJAX to improve the use experience. In the Java side, the choice for me is very clear, i would use DWR (of course), but in PHP side i decided to use an agnostic Javascript library, i’m going to use Dojo Toolkit.

This is a flow of whole process, how the several libraries and APIs interconnect.
Create a web service
The first step is create the web service in Java space. I use Tomcat and Axis to complete this task. You can use your favourite IDE, like Eclipse WTP or Netbeans to get it working with a wizard, but i’m going to do it manually.
Visit the Axis website and download the latest version (at this time is 1.4).
Create a new web project, copy all .jar files from axis binary package, and the deployment descriptor file (web.xml).
Then you have to create your Java class to expose with axis as a web service. I decide to make a simple Calculator class like this:
package com.martincuervo.calc;
public class SimpleCalc {
public int add(int i, int j) {
return i + j;
}
}
Then i copied several needed files to deploy this service.
- deploy.wsdd this files is needed to define which class you want to use in your service and which methods are going to be accesible
- undeploy.wsddconfig file used in the undeploy
- build.xml, the main ant script file, used to start deploy and undeploy process
deploy.wsdd
<deployment xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<service name="CalcWS" provider="java:RPC">
<parameter name="className" value="com.martincuervo.calc.SimpleCalc"/>
<parameter name="allowedMethods" value="add"/>
</service>
</deployment>
undeploy.wsdd
<undeployment xmlns="http://xml.apache.org/axis/wsdd/">
<service name="CalcWS"/>
</undeployment>
build.xml
<project name="CalcWS" default="deploy">
<path id="axis.classpath">
<fileset dir="WebContent/WEB-INF/lib">
<include name="**/*.jar" />
</fileset>
</path>
<taskdef
classname="org.apache.axis.tools.ant.axis.AdminClientTask"
name="AdminClient"
classpathref="axis.classpath" />
<target name="deploy">
<AdminClient
servletpath="calcws/servlet/AxisServlet"
xmlfile="deploy.wsdd"/>
</target>
<target name="undeploy">
<AdminClient
servletpath="calcws/servlet/AxisServlet"
xmlfile="undeploy.wsdd"/>
</target>
</project>
Execute the web service
Now we need a PHP library to execute this web service. Visit the NuSOAP web site and download the lastest version (at this time 0.7.2)
This is the code you need to execute the Calculator service:
<?php
require_once('lib/nusoap.php');
$client = new soapclient(
'http://localhost:8080/calcws/services/CalcWS?wsdl', true);
$result = $client->call('add', array('i' => 2, 'j' => 3));
echo $result;
?>
I think this code is pretty straightforward and self explanatory.