关闭

SOAP used in java -----First Example

651人阅读 评论(0) 收藏 举报

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!

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:67508次
    • 积分:1235
    • 等级:
    • 排名:千里之外
    • 原创:50篇
    • 转载:20篇
    • 译文:0篇
    • 评论:8条
    最新评论