Advertisment

Web Services on PCQLinux 2004

author-image
PCQ Bureau
New Update

Setting up PCQLinux 2004 to host Java-based Web services is a matter of installing a package called Axis, which is given out on this month’s PCQXtreme CD. With Axis, publishing Web services is as easy as copying a Java source code file into a prescribed directory. No specific coding is required, neither is the source code required to be compiled. It also allows you to deploy compiled Java code as Web services and convert an existing Web application into Web services. 

Advertisment

We will look at the first and the easiest way of deploying a Web service. We assume that you have installed the Tomcat app server during the PCQLinux 2004 installation. If not, install the RPM named tomcat-5.0.16-0.i386.rpm found on CD2 of PCQLinux 2004. Tomcat gets installed into the directory /opt/tomcat. We will call this directory as the Tomcat’s installation directory. 

Snapshot
Applies to Java developers
Usp Useful for quickly coding Web services in Java
Links ws.apache.org/axis/,

java.sun.com,  jakarta.apache.org

Install Axis



Copy the file axis-1_1.tar.gz from this month’s CD to /opt directory on PCQLinux 2004. Extract the archive as:








tar -zxvf axis-1_1.tar.gz








This will produce a directory named axis-1_1 under /opt. Issue the following command to copy the axis subdirectory to the Tomcat installation’s webapp directory.





cp -R /opt/ axis-1_1/webapps/axis /opt/tomcat/webapps







Start Tomcat by issuing the command 





‘/etc/rc.d/init.d/tomcat start’. 



Advertisment

If you get an error of JAVA_HOME variable not defined, issue the following:






export JAVA_HOME=/usr/java/j2sdk1.4.2_03







Next, you must again issue the previous command to start Tomcat. Type in the URL http://127.0.0.1:8080/axis/ in the Web browser (Mozilla or Konqueror).Click on the validate link. Download missing ‘Needed components’ that Axis complains about. For example, Axis will complain about a missing activation.jar, in which case, download the file from the indicated URL and copy it to the directory /opt/tomcat/webapps/axis/WEB-INF/lib. Stop and start Tomcat as:





/etc/rc.d/init.d/tomcat stop


/etc/rc.d/init.d/tomcat start








Then, you must set up environment variables named AXISLIB and AXISCLASSPATH as:





export AXISLIB=/opt/tomcat/webapps/axis/WEB-INF/lib/export





AXISCLASSPATH=$AXISLIB/activation.jar:$AXISLIB/commons-discovery.jar:$AXISLIB/log4j-1.2.8.jar:$AXISLIB/axis-ant.jar:$AXISLIB/commons-logging.jar:$AXISLIB



/saaj.jar:$AXISLIB/axis.jar:$AXISLIB/jaxrpc.jar:$AXISLIB


/wsdl4j.jar





Now, you get down to coding the Web service. 

The Web service



With Tomcat and Axis, the easiest way to deploy a Web service is to simply copy a Java source code file into the subdirectory webapps/axis of the Tomcat’s installation. Once copied, rename the file to have a .jws extension instead of .java. This will publish all the public methods (defined in the source code file) as a Web service. Consider the following code of a simple Java class called AWeb

Service.






public class AWebService{


public String getHello(){


return “Hello from A Web Service”;


}


public String getHello(String name){


return “Hello “+name+ “, greetings from A Web Service”;


}


};














The class has two methods named getHello( ). The first one accepts no parameter whereas the second getHello( ) accepts a string parameter. Both will return a string, when called. Type in the above code in a text editor and save the file as AWebService.jws. Copy this file to the directory /opt/tomcat/webapps/axis. All done. Both the versions of the getHello( ) method are now available as a Web service to be called by a client. 

Advertisment

The client side



The code for the client to consume the above deployed Web service is as follows:






import org.apache.axis.client.Call;


import org.apache.axis.client.Service;


import javax.xml.namespace.QName;


import java.net.*;


public class WebServiceClient {


public static void main(String <> args) throws Exception{


Service service = new Service();


Call call = (Call)service.createCall();


String endpoint = “http://127.0.0.1:8080/axis/AWebService. jws”;


call.setTargetEndpointAddress(new URL(endpoint));


call.setOperationName(new QName(“getHello”));


String result = (String)call.invoke(new Object<>{“shekhar”});


System.out.println(“Result : “ + result);


}


}





















The code starts with importing some Axis packages. All these packages reside in jar file found in the directory webapps/axis/WEB-INF/lib. With the statement:






String endpoint = “http://127.0.0.1:8080/axis/AWebService.jws”;









We specify the URL to the Web service and with the statement:






call.setOperationName(new Qname(“getHello”));







We specify the name of the method (getHello) we intend to call. Finally, the statement:





String result = (String)call.invoke(new
Object<>{“shekhar”});








Is used to pass the parameters to the method and also invoke (call) the method. Note that the parameters are passed as an Object array and the return value is a generic Java object. So it must be cast back to the specific type (or Object), which is string in this case. To call the first version of getHello( ) which does not accept any parameter, modify the statement to:





String result = (String)call.invoke(new Object<>{“”});







Note that even if the method does not accept any parameters, you still have to construct an Object array with a empty string as - new Object<>{“”}. 






Type in the code for the client in a text editor and save the file as WebServiceClient.java. Now, compile the client as:






javac -classpath $AXISCLASSPATH WebServiceClient.java







To execute the client:





java -cp $AXISCLASSPATH:. WebServiceClient







You should get a greeting message from the Web Service. Next month we will look at how to deploy a compiled Java code as a Web service and how to convert an existing Web application into a Web server. 


Shekhar Govindarajan 



IT4Enterprise

Advertisment