Web Services 可以将应用程序转换为网络应用程序。
通过使用 Web Services,您的应用程序可以向全世界发布信息,或提供某项功能。
Web Services 可以被其他应用程序使用。
通过 Web Services,您的会计部门的 Win 2k 服务器可以与 IT 供应商的 UXIX 服务器相连接。
基本的 Web Services 平台是 XML+HTTP。
Web services 使用 XML 来编解码数据,并使用 SOAP 来传输数据。
参考文献及推荐阅读:
http://dev.rdxx.com/Java/WebService/2005-07/27/102648552.shtml
http://www.onjava.com/pub/a/onjava/2002/02/27/tomcat.html?page=1
1 下载SOAP
http://apache.mirror.phpchina.com/ws/axis/1_4/axis-bin-1_4.zip
2 xerces
http://xml.apache.org/dist/xerces-j/Xerces-J-bin.2.9.1.zip
3 JavaMail
http://cds.sun.com/is-bin/INTERSHOP.enfinity/WFS/CDS-CDS_Developer-Site/en_US/-/USD/VerifyItem-Start/javamail-1_4_1.zip?BundledLineItemUUID=9p5IBe.pm1wAAAEZgx8LhFpM&OrderID=G6xIBe.pZPgAAAEZdh8LhFpM&ProductID=3v_ACUFBMTsAAAEYxBQ5AXuF&FileName=/javamail-1_4_1.zip
4 JSF(JavaBean Activation Framework)
http://cds.sun.com/is-bin/INTERSHOP.enfinity/WFS/CDS-CDS_Developer-Site/en_US/-/USD/VerifyItem-Start/jaf-1_1_1.zip?BundledLineItemUUID=d.JIBe.mX1cAAAEZvmRIclpM&OrderID=dDBIBe.mM7MAAAEZsWRIclpM&ProductID=zFnACUFBlVoAAAEYhxc5AXt.&FileName=/jaf-1_1_1.zip
http://127.0.0.1:8080/soap/servlet/rpcrouter
出现如下页面:
SOAP RPC Router
Sorry, I don't speak via HTTP GET- you have to use HTTP POST to talk to me.
复制相关的*.jar进入Tomcat
D:/Tomcat/common/lib/soap.jar;
D:/Tomcat/common/lib/mail.jar;
D:/Tomcat/common/lib/xml-apis.jar;
D:/Tomcat/common/lib/activation.jar;
D:/Tomcat/common/lib/xercesImpl.jar
1 创建HelloWorldService.java
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
package onjava;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
public class HelloWorldService
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public String getMessage()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
return "Hello World!";
![](https://i-blog.csdnimg.cn/blog_migrate/717446ca04a6125dc5b6b54e0fa14ab4.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/0196c3df5ea9e936f21e9932cca91014.gif)
}
2 HelloWorldClient.java
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import java.io.*;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import java.util.*;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import java.net.*;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import org.apache.soap.*;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import org.apache.soap.rpc.*;
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public class HelloWorldClient {
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public static String DEFAULT_ENDPOINT = "http://localhost:8080/soap/servlet/rpcrouter";
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public static void main(String args[]) throws Exception {
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
String endPoint = DEFAULT_ENDPOINT;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//Process Arguments
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
if (args.length == 1)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
endPoint = args[0];
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
else if (args.length > 1)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
System.out.println("java HelloWorldClient [endpoint]");
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// Build the SOAP RPC request message using the Call object
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Call call = new Call();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
call.setTargetObjectURI("urn:onjavaserver");
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
call.setMethodName("getMessage");
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// Create a URL object, which represents the endpoint
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
URL url = new URL(endPoint);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// Send the SOAP RPC request message using invoke() method
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Response resp = call.invoke(url, "");
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// Check the response.
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (resp.generatedFault()) { // Error Occured
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Fault fault = resp.getFault();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
System.out.println("The Following Error Occured: ");
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
System.out.println(" Fault Code = " + fault.getFaultCode());
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
System.out.println(" Fault String = " + fault.getFaultString());
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} else { // Completed Successfully
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Parameter result = resp.getReturnValue();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
System.out.println(result.getValue());
![](https://i-blog.csdnimg.cn/blog_migrate/717446ca04a6125dc5b6b54e0fa14ab4.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/717446ca04a6125dc5b6b54e0fa14ab4.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/0196c3df5ea9e936f21e9932cca91014.gif)
}
3 Tomcat下的配置:
Deployed Service Information
'urn:onjavaserver' Service Deployment Descriptor |
---|
Property | Details |
---|
ID | urn:onjavaserver |
Scope | Application |
Provider Type | java |
Provider Class | onjava.HelloWorldService |
Use Static Class | false |
Methods | getMessage |
Type Mappings | |
Default Mapping Registry Class | |
运行:
java HelloWorldClient
Hello World!