Tuesday, March 31, 2009

Creating The Web Services Client (Part 2)

The web services can be accessed by anykind of applications such as desktop application, web application, even mobile application. In the previous tutorial, we have learnt how to create a desktop application to access the web service. Now, we are going to create a web application with PHP to access that web service.

The Code

This is the PHP script. But, there is a requirement before you can run this script. You need a supporting library named NuSOAP. You can find it from www.sourceforge.net.

NuSOAP came in a zipped file. So, unzip it and copy the nusoap.php file to the same location with this script so it can be included with this script.

<html>
    <head><title>Web Service Client</title></head>
<body>
    <form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
        <input type="text" name="name"><input type="submit" name="submit" value="SUBMIT">
    </form>
    <div id="result" style="font-weight:bold;">
    <?php
    if($_POST['submit']){
        include("nusoap.php");
        
        // THE LOCATION OF WEB SERVICES
        $client=new soap_client("http://localhost:8080/axis/services");
        
        // CALL THE WEB SERVICE WITH THE METHOD NAME, INPUT PARAMETER, AND SERVICE NAME
        $result = $client->call('greeting', array("name" => $_POST['name']), 'helloService');
        if($client->getError()){
            echo $client->getError();
        }
        else{
            echo $result;
        }
    }
    ?>
    </div>
</body>
</html>

Run It!

This is the screenshot of this application while running.


Monday, March 16, 2009

Creating The Web Services Client (Part 1)

In the previous tutorial, we've learnt how to create a simple web service with Apache Axis. Now, we're going to learn how to create a simple client application to access that web service. This application is a simple desktop application made with Java programming language.

The Code

This is the source code of this application. Save this file with name helloClient.java

import org.apache.axis.AxisFault;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;
import org.apache.axis.encoding.XMLType;
import javax.xml.rpc.ParameterMode;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class helloClient extends JFrame implements ActionListener{
    private JButton btnSend;
    private JTextField txtParameter;
    
    public void initComponent(){
        btnSend=new JButton("SEND");
        btnSend.setMnemonic('S');
        btnSend.addActionListener(this);
        txtParameter=new JTextField(10);
    }
    
    public void design(){
        setTitle("Web Service Client");
        getContentPane().setLayout(new FlowLayout());
        getContentPane().add(txtParameter);
        getContentPane().add(btnSend);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }
    
    private String sendRequest(String name) throws Exception{
        Service  service = new Service();
        Call call = (Call)service.createCall();
        String result;
        
        // SET THE LOCATION OF WEB SERVICE
        String endpoint = "http://localhost:8080/axis/services";
        call.setTargetEndpointAddress( new java.net.URL(endpoint));

        // SET THE SERVICE NAME AND THE METHOD TO BE CALLED
        call.setOperationName(new QName("helloService", "greeting"));
        
        // ADD THE INPUT PARAMETER
        call.addParameter( "name", XMLType.XSD_STRING, ParameterMode.IN );
        
        // SEND REQUEST TO WEB SERVICE
        String response = (String) call.invoke(new Object[]{name});

        return response;
    }
    
    helloClient(){
        initComponent();
        design();
    }
    
    public static void main(String [] args){
        new helloClient();
    }

    public void actionPerformed(ActionEvent ae){
        if(ae.getSource()==btnSend){
            try{
                JOptionPane.showMessageDialog(this,sendRequest(txtParameter.getText()), "Response", JOptionPane.INFORMATION_MESSAGE);
            }
            catch(Exception ex){
                JOptionPane.showMessageDialog(this,"Error : " + ex.toString(), "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
}

Compile

This application needs some Axis' libraries. So, don't forget to set the required environment variables. I have write about this in the previous tutorial. After that, compile the helloClient.java file with this command.

javac -classpath %AXISCLASSPATH% helloClient.java

Run

If your compilation process didn't output any error, you can run this application with this command.

java -classpath .;%AXISCLASSPATH% helloClient

This is the screenshot of this application.


Monday, March 9, 2009

Creating Web Services With Apache Axis

The Environment Variables

Before you start, you have to set up some environment variables. You can set them from Windows System Properties or using the MS-DOS batch file (.bat)like this.

set AXIS_HOME=C:\apache-tomcat\webapps\axis\WEB-INF
set AXIS_LIB=%AXIS_HOME%\lib
set AXISCLASSPATH=%AXIS_LIB%\axis.jar;%AXIS_LIB%\commons-discovery-0.2.jar;%AXIS_LIB%\commons-logging-1.0.4.jar;%AXIS_LIB%\jaxrpc.jar;%AXIS_LIB%\log4j-1.2.8.jar;%AXIS_LIB%\saaj.jar;%AXIS_LIB%\wsdl4j-1.5.1.jar;%AXIS_LIB%\activation.jar;%AXIS_LIB%\mail.jar;%AXIS_LIB%\xmlsec.jar

If you are using Linux, you can use the following executable shell script (.sh) to set those environment variables.

#!/bin/sh
AXIS_HOME="/usr/local/apache-tomcat/webapps/axis/WEB-INF"; export AXIS_HOME;
AXIS_LIB="$AXIS_HOME/lib"; export AXIS_LIB;
AXISCLASSPATH="$AXIS_LIB/axis.jar:$AXIS_LIB/commons-discovery-0.2.jar:$AXIS_LIB/commons-logging-1.0.4.jar:$AXIS_LIB/jaxrpc.jar:$AXIS_LIB/log4j-1.2.8.jar:$AXIS_LIB/saaj.jar:$AXIS_LIB/wsdl4j-1.5.1.jar:$AXIS_LIB/activation.jar:$AXIS_LIB/mail.jar:$AXIS_LIB/xmlsec.jar"; export AXISCLASSPATH;

The Web Service

To create a web service with Apache Axis, you have to put all your code under webapps/axis/WEB-INF/classes folder. To make your project seems more organize, you can create one folder for each project. In this example I will create a folder named hello to store my files. And, this is the source code of helloService.java.

package hello;

public class helloService{
    public static String greeting(String name){
        return "Hello, " + name + "!";
    }
}

Compile it with this command.

javac helloService.java

Deploying Web Service

After compiling your web service, you need to deploy it. Before deploying it, you need to create a file called deploy.wsdd.

<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
    <service name="helloService" provider="java:RPC">
        <parameter name="className" value="hello.helloService"/>
        <parameter name="allowedMethods" value="*"/>
    </service>
</deployment>

Now, you can deploy it with this command (Don't forget to set the required environment variables before deploying it).

java -classpath %AXISCLASSPATH% org.apache.axis.client.AdminClient deploy.wsdd

Or with this command if you are using Linux.

java -classpath $AXISCLASSPATH org.apache.axis.client.AdminClient deploy.wsdd

Open the address http://localhost:8080/axis/services/helloService?wsdl with your web browser. This is the WSDL document of your helloService web service. If you see this, it means you have succesfully created your web service with Apache Axis.


Undeploying Web Service

To undeploy your web service, you have to create a file named undeploy.wsdd.

<undeployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
    <service name="helloService" />
</undeployment>

Undeploy it with this command (Once again, don't forget to set the required environment variables before undeploying it).

java -classpath %AXISCLASSPATH% org.apache.axis.client.AdminClient undeploy.wsdd

Or with this command if you are using Linux.

java -classpath $AXISCLASSPATH org.apache.axis.client.AdminClient undeploy.wsdd

Monday, March 2, 2009

Installing Apache Axis

Apache Axis is a very usefull software if you want to create web services with JSP. Apache Tomcat must be installed first if you want to install Apache Axis. In this tutorial, I assume you have installed Apache Tomcat. If you haven't installed Apache Tomcat, you can read my other tutorial about installing Apache Tomcat.

These are some steps for installing Apache Axis.

Supporting Libraries

Before installing Apache Axis, you also need these files as the supporting libraries.

  1. activation.jar
  2. mail.jar
  3. xmlsec.jar

Installing Apache Axis

  1. Download the latest version of Apache Axis from ws.apache.org/axis.
  2. Extract the axis-bin-1_4.zip in anywhere you like.
  3. Copy the axis folder inside the webapps folder of the Axis distribution folder, to your Apache Tomcat webapps folder.
  4. Extract those 3 supporting libraries and copy the .jar files to the lib folder inside the WEB-INF folder of your Axis installation folder.
  5. Start your Apache Tomcat or restart it if you've got it running.
  6. Access http://localhost:8080/axis. If you see the result like below this, it means you have installed Apache Axis successfully. You can also click the Validation link to check if your Axis' installation configuration is fine.