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.


No comments: