您的位置:首页 > 编程语言 > Java开发

Java/WebService 入门与使用

2007-08-28 12:32 281 查看
一、下载
我们可以从学习Axis开始。

Axis is essentially a SOAP engine -- a framework for constructing SOAP processors such as clients, servers, gateways, etc. The current version of Axis is written in Java, but a C++ implementation of the client side of Axis is being developed.

SOAP is an XML-based communication protocol and encoding format for inter-application communication.

到Apache的网站就可以下载到axis

二、准备开发
2.1 开发环境
使用Eclipse建立Dynamic Web Applicaiton项目"wsexample",目录如下
* 源路径 src/webservice
* 目标路径 WebContent/WEB-INF/classes
* 库路径 WebContent/WEB-INF/lib

把下列文件复制到库路径中
axis.jar
commons-discovery-0.2.jar
commons-logging-1.1.jar
jaxrpc.jar
saaj.jar
wsdl4j-1.5.1.jar
xerces.jar

并且把这些*.jar添加到Build Path

2.2 新建一个测试类(服务端)
package samland.webservice.demo;

public class HelloWorld {
public String sayHello(){
return "Hello world!";
}

public String echo(String u){
return "Hello " + u;
}
}

2.3 建立一个测试类(客户端)
package samland.webservice.demo;

import javax.xml.namespace.QName;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;

public class TestHello {
public static void main(String [] args) {
try {
String endpoint =
"http://localhost:8080/wsexample/services/HelloWorld";

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

call.setTargetEndpointAddress( new java.net.URL(endpoint) );
call.setOperationName(new QName("http://demo.webservice.samland/", "echo"));

String ret = (String) call.invoke( new Object[] { "Samland" } );

System.out.println("Sent 'Hello!', got '" + ret + "'");
} catch (Exception e) {
System.err.println(e.toString());
}
}

}

三、发布webservice服务
3.1 新建一个WebService

New->Other...
Web Services->Web Service

Next:
选择一个WebService服务类,即HelloWorld。
Service implementation: samland.webservice.demo.HelloWorld
Configuration:
Server: Tomcat v5.5
Web service runtime: Apache Axis
Service project:wsexample

Next: 下一步
Finish: 完成

Eclipse将会启动Tomcat v5.5
打开浏览器访问http://localhost:8080/wsexample/services/HelloWorld?wsdl即可看到已经发布的WebService描述

3.2 运行TestHello,成功的话就可以看到
Sent 'Samland', got 'Hello Samland'

完成。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: