原文:http://www.cnblogs.com/hoojo/archive/2010/12/20/1911357.html
1、在官方网站下载axis的工程(这个等下就有用的)和源码、jar包等,下载地址是:
http://labs.renren.com/apache-mirror//ws/axis/1_4/
2、解压下载的工程或源码(两个中任意一个都可以),解压axis-bin-1.4可以看到大致目录是这样的:

docs是文档、lib是jar包、sample是示例、xmls是当前工程所需的xml、webapps是当前工程的webroot目录;
我们打开webapps目录就可以看到一个axis的文件夹,这个文件夹里面有WEB-INF文件夹和一些页面,将axis复制到你的tomcat的webapps目录下。然后启动tomcat服务,访问http://localhost:8080/axis/,看到下面的解码就说明部署成功了:

以后我们将和这个工程不离不弃,它将在我们的axis1.x的webService中发挥很大的作用!
3、创建我们自己的web工程,这里我新建的AxisWebService;创建好工程后,将刚才解压的axis-bin中的lib的jar包copy到当前工程的lib中;
axis-ant.jar
axis.jar
commons-discovery-0.2.jar
commons-logging-1.0.4.jar
jaxrpc.jar
log4j-1.2.8.jar
saaj.jar
wsdl4j-1.5.1.jar
activation-1.1.jar
mail-1.4.jar
创建webService类文件,代码如下:
package
com.hoo.service;
/**
* <b>function:</b>jws的axis WebService *
@author
hoojo * @createDate Dec 15,2010 17:03:49 PM * @file HelloWorldService.java * @package com.hoo.service * @project AxisWebService * @blog
http://blog.csdn.net/IBM_hoojo
* @email hoojo_@126.com *
@version
1.0
*/
public
class
HelloWorldService {
public
String sayHello(String name,
int
age) {
return
name
+
"
say : hello world! [axis] my age is
"
+
age; } }
4、复制HelloWorldService.java到我们刚才复制的axis文件夹下即可;也就是tomcat下的webapps下的axis下即可;注意:还有重要的一般就是要将这个java文件中的包名去掉,并且将这个文件重命名为HelloWorldService.jws;如果带包名的话,请求后编译的class将会在包路径下,这样我们在全球当前jws的时候就会出现找不到class,详细的你可以到发布在tomcat下的工程看看WEB-INF目录下的jwsClass就一目了然了。
上面的工作完成后,启动tomcat服务器,访问http://localhost:8080/axis/HelloWorldService.jws
你会看到:
There is a Web Service here
Click to see the WSDL
如果你和我看到的是一样的,就证明你已经成功的部署了一个axis1.x的webService。然后我们点击下就可以看到wsdl的xml文件了,内容如下:

<?
xml version="1.0" encoding="UTF-8"
?>
-
<
wsdl:definitions
targetNamespace
="http://localhost:8080/axis/HelloWorldService.jws"
xmlns:apachesoap
="http://xml.apache.org/xml-soap"
xmlns:impl
="http://localhost:8080/axis/HelloWorldService.jws"
xmlns:intf
="http://localhost:8080/axis/HelloWorldService.jws"
xmlns:soapenc
="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:wsdl
="http://schemas.xmlsoap.org/wsdl/"
xmlns:wsdlsoap
="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:xsd
="http://www.w3.org/2001/XMLSchema"
>
-
<!--
WSDL created by Apache Axis version: 1.4 Built on Apr 22,2006 (06:55:48 PDT)
-->
-
<
wsdl:message
name
="sayHelloResponse"
>
<
wsdl:part
name
="sayHelloReturn"
type
="xsd:string"
/>
</
wsdl:message
>
-
<
wsdl:message
name
="sayHelloRequest"
>
<
wsdl:part
name
="name"
type
="xsd:string"
/>
<
wsdl:part
name
="age"
type
="xsd:int"
/>
</
wsdl:message
>
-
<
wsdl:portType
name
="HelloWorldService"
>
-
<
wsdl:operation
name
="sayHello"
parameterOrder
="name age"
>
<
wsdl:input
message
="impl:sayHelloRequest"
name
="sayHelloRequest"
/>
<
wsdl:output
message
="impl:sayHelloResponse"
name
="sayHelloResponse"
/>
</
wsdl:operation
>
</
wsdl:portType
>
-
<
wsdl:binding
name
="HelloWorldServiceSoapBinding"
type
="impl:HelloWorldService"
>
<
wsdlsoap:binding
style
="rpc"
transport
="http://schemas.xmlsoap.org/soap/http"
/>
-
<
wsdl:operation
name
="sayHello"
>
<
wsdlsoap:operation
soapAction
=""
/>
-
<
wsdl:input
name
="sayHelloRequest"
>
<
wsdlsoap:body
encodingStyle
="http://schemas.xmlsoap.org/soap/encoding/"
namespace
="http://DefaultNamespace"
use
="encoded"
/>
</
wsdl:input
>
-
<
wsdl:output
name
="sayHelloResponse"
>
<
wsdlsoap:body
encodingStyle
="http://schemas.xmlsoap.org/soap/encoding/"
namespace
="http://localhost:8080/axis/HelloWorldService.jws"
use
="encoded"
/>
</
wsdl:output
>
</
wsdl:operation
>
</
wsdl:binding
>
-
<
wsdl:service
name
="HelloWorldServiceService"
>
-
<
wsdl:port
binding
="impl:HelloWorldServiceSoapBinding"
name
="HelloWorldService"
>
<
wsdlsoap:address
location
="http://localhost:8080/axis/HelloWorldService.jws"
/>
</
wsdl:port
>
</
wsdl:service
>
</
wsdl:definitions
>

分析下wsdl的xml文件内容:
targetNamespace=http://localhost:8080/axis/HelloWorldService.jws
是我们部署的webservice命名空间,也就是我们访问的webService路径。
<wsdl:message name="sayHelloResponse">
<wsdl:part name="sayHelloReturn" type="xsd:string" />
</wsdl:message>
是返回值的信息,sayHelloResponse代表响应,即返回值,type是返回值的类型
<wsdl:message name="sayHelloRequest">
<wsdl:part name="name" type="xsd:string" />
<wsdl:part name="age" type="xsd:int" />
</wsdl:message>
请求方法参数信息,sayHelloRequest即请求,part是参数parameter,type是参数的类型
<wsdl:portType name="HelloWorldService">
<wsdl:operation name="sayHello" parameterOrder="name age">
<wsdl:input message="impl:sayHelloRequest" name="sayHelloRequest" />
<wsdl:output message="impl:sayHelloResponse" name="sayHelloResponse" />
</wsdl:operation>
</wsdl:portType>
portType的name是当前webService的名称,operation是一个操作,即可以调用的方法。name就是方法名称了,parameterOrder是参数,input输入即传入参数,output输出即返回的值;
<wsdl:service name="HelloWorldServiceService">
<wsdl:port binding="impl:HelloWorldServiceSoapBinding" name="HelloWorldService">
<wsdlsoap:address location="http://localhost:8080/axis/HelloWorldService.jws" />
</wsdl:port>
</wsdl:service>
webService的名称和绑定的信息,以及访问的url地址。
5、下面编写客户端代码
代码如下:

package
com.hoo.client;
import
java.rmi.RemoteException;
import
javax.xml.namespace.QName;
import
javax.xml.rpc.ServiceException;
import
org.apache.axis.client.Call;
import
org.apache.axis.client.Service;
public
class
HelloWorldClient {
/**
* <b>function:</b>jws axis WebService客户端 *
@author
hoojo * @createDate 2010-12-15 下午05:10:28 *
@param
args *
@throws
ServiceException *
@throws
RemoteException
*/
public
static
void
main(String[] args)
throws
ServiceException,RemoteException {
//
webService访问地址
//
String url = "
http://localhost
:8080/axis/HelloWorldService.jws";
String url
=
"
http://localhost:8080/AxisWebService/HelloWorldService.jws
"
;
//
创建服务
Service service
=
new
Service();
//
创建调用句柄
Call call
=
(Call) service.createCall();
//
设置请求地址
call.setTargetEndpointAddress(url);
/**
* 设置调用的方法和方法的命名空间; * 因为这里是手动发布到webroot目录下的,所以命名空间和请求地址一致 * 当然null也可以,因为本身它就没有设置命名空间,一般方法的命名空间是 * 包名倒写组成,如com.hoo.service,ns=
http://service.hoo.com
*/
call.setOperationName(
new
QName(
null
,
"
sayHello
"
));
/**
* 用call调用sayHello方法,设置请求的参数,返回的就是返回值了
*/
String result
=
(String) call.invoke(
new
Object[] {
"
jack
"
,
99
}); System.out.println(result); } }

分析上面的代码
url是根据xml文件中的wsdlsoap:address location的信息得到的,命名空间和方法名称是根据
<
wsdl:operation
name
="sayHello"
>
<
wsdlsoap:operation
soapAction
=""
/>
-
<
wsdl:input
name
="sayHelloRequest"
>
<
wsdlsoap:body
encodingStyle
="http://schemas.xmlsoap.org/soap/encoding/"
namespace
="http://DefaultNamespace"
use
="encoded"
/>
</
wsdl:input
>
-
<
wsdl:output
name
="sayHelloResponse"
>
<
wsdlsoap:body
encodingStyle
="http://schemas.xmlsoap.org/soap/encoding/"
namespace
="http://localhost:8080/axis/HelloWorldJWS.jws"
use
="encoded"
/>
</
wsdl:output
>
的信息得到的,而请求参数和返回值的详细信息是在
<
wsdl:message
name
="sayHelloRequest"
>
<
wsdl:part
name
="name"
type
="xsd:string"
/>
<
wsdl:part
name
="age"
type
="xsd:int"
/>
</
wsdl:message
>
-
<
wsdl:message
name
="sayHelloResponse"
>
<
wsdl:part
name
="sayHelloReturn"
type
="xsd:string"
/>
</
wsdl:message
>
-
<
wsdl:portType
name
="HelloWorldJWS"
>
-
<
wsdl:operation
name
="sayHello"
parameterOrder
="name age"
>
<
wsdl:input
message
="impl:sayHelloRequest"
name
="sayHelloRequest"
/>
<
wsdl:output
message
="impl:sayHelloResponse"
name
="sayHelloResponse"
/>
</
wsdl:operation
>
</
wsdl:portType
>
里可以很详细的看到。
至于代码的call.invoke是java中反射机制,不懂的建议看看jdk文档java.lang.reflect包下的内容。
运行上面的代码就可以看到控制台输出:
jack say : hello world! [axis] my age is 99
好了,axis的就完成了,下面我们不用官方的axis的工程,我们写一个自己的AxisWebService工程,然后发布的tomcat的webapps中看看。
6、刚才copy了lib下的jar包,现在要copy下web.xml中的内容,去掉里面的AdminServlet这个配置,其他的都可保留。
然后像刚才一样,将HelloWorldService.java复制到webroot目录下,去掉包名,并且修改后缀为HelloWorldService.jws即可。(如果有兴趣可以看看,发布在tomcat目录下的当前工程的web-inf目录,看看里面是否多了些东西)最后发布当前web工程,访问http://localhost:8080/AxisWebService/HelloWorldService.jws,如果看到和刚才一样的界面,证明你快成功了。点击链接看到wsdl的xml就成功了。
好了,还没有完。看看web.xml中的配置,你大概就知道为什么了。
web.xml中最主要的文件就是org.apache.axis.transport.http.AxisServlet,它就是webService的中央控制器;即配置jws的后缀也在web.xml中定义的,在看看还有services/*,这就表明上面的访问路径也可以是这样的:http://localhost:8080/AxisWebService/services/HelloWorldService
当然如果要这样写就需要用wsdd的发布方式,详细请看下文!
参考文档:http://ws.apache.org/axis/java/user-guide.html