Java/WebService 入门与使用

 

 

<script type="text/javascript"> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>

一、下载
我们可以从学习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'

完成。 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值