smily的专栏

技术交流

SOAP used in java -----First Example

First  we need  :

JAVA 2 SDK 1.4.1,Apache SOAP 2.3.1,JAF 1.0.2,JAVAMAIL 1.3.1, Xerces 2.6.0 , Tomcat 5.0Java SDK,JAF和JAVAMAIL(http://java.sun.com),the other you can down them from(http://jakarta.apache.org)

Second we need set some classpath, listed as follows:

set CATALINA_HOME=C:/Tomcat 5.0
set CLASSPATH=%JAVA_HOME%/lib/tools.jar
set CLASSPATH=%JAVA_HOME%/[your own class drectory ]/soap.jar
set CLASSPATH=%CLASSPATH%;%JAVA_HOME%/  [your own class drectory ]/mail.jar
set CLASSPATH=%CLASSPATH%;%JAVA_HOME%/[your own class drectory ]/activation.jar
set CLASSPATH=%CLASSPATH%;%JAVA_HOME%/[your own class drectory ]/xercesImpl.jar
set CLASSPATH=%CLASSPATH%;%JAVA_HOME%/[your own class drectory ]/xercesSamples.jar
set CLASSPATH=%CLASSPATH%;%JAVA_HOME%/[your own class drectory ]/xml-apis.jar
set CLASSPATH=%CLASSPATH%;%JAVA_HOME%/[your own class drectory ]/xmlParserAPIs.jar
set CLASSPATH=%CLASSPATH%;%CATALINA_HOME%/common/lib/servlet.jar
set CLASSPATH=%CLASSPATH%;%CATALINA_HOME%/common/lib/tools.jar

Third Class we use here

package com.test.SOAPTest;

public class SOAPService {
 public String sayHi(String x) {
  return ("Hello my friend, " + x + "! Glad to see you!");
 }
}

then open tomcat ,type the address http://localhost:8080/soap/   , run "Run the admin client ", and input:

ID: urn:HelloWorld_SOAPService
Scope: Application
Methods: sayHi
Provider Type: java
Java Provider - Provider Class: HelloWorld.SOAPService
Java Provider - Static? No


ok,then you can copy the class SOAPService to [Tomcat_home]/common/classes ,

now ,we create the SOAPClient  here,you can see the detail

package com.test.SOAPTest;

import java.net.*;
import java.util.*;
import org.apache.soap.*;
import org.apache.soap.rpc.*;

public class HelloWorldClient {
 public static void main(String[] arg) throws Exception {
  Call c = null;
  URL url = null;
  Vector params = null;
  Response rep = null;
  String ourName = "Superman";
  String ourUrn = "urn:HelloWorld_SOAPService";
  String ourMethod = "sayHi";
  url = new URL("http://localhost:8080/soap/servlet/rpcrouter");
  System.out.println("Passing to our deployed " + ourUrn + "our name ("
    + ourName + "): ");
  c = new Call();
  c.setTargetObjectURI(ourUrn);
  c.setMethodName(ourMethod);
  c.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
  params = new Vector();
  params
    .addElement(new Parameter("ourName", String.class, ourName,
      null));
  c.setParams(params);
  System.out.print("and its answer is: ");
  rep = c.invoke(url, "");
  if (rep.generatedFault()) {
   Fault fault = rep.getFault();
   System.out.println("/nCall failed!");
   System.out.println("Code = " + fault.getFaultCode());
   System.out.println("String = " + fault.getFaultString());
  } else {
   Parameter result = rep.getReturnValue();
   System.out.print(result.getValue());
   System.out.println();
  }
 }
}

Till now we have finished all the work ,then we can run it.

Run tomcat and test our client ,we will see the result " hello my friend,superman,Glad to see you!"

Over!

阅读更多
个人分类: JAVA/J2EE
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

SOAP used in java -----First Example

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭