axis第一个例子

首先安装axis和tomcat6,将axis安装目录下webapps下的axis拷贝到tomcat的webapps目录中去,并在axis\web-info\lib目录下拷贝可选的jar包
然后编写helloclient代码,然后将其拷贝到D:\Tomcat6\webapps\axis下,并命名为HelloClient.jws。

public class HelloClient { public String getName(String name) { return "hello "+name; } } 

启动tomcat,在浏览器输入:http://localhost:8080/axis/HelloClient.jws
显示以下结果:
There is a Web Service here
Click to see the WSDL
点击,可查看wsdl文件
或者输入http://localhost:8080/axis/HelloClient.jws?wsdl
  <?xml version="1.0" encoding="UTF-8" ?> 
- <wsdl:definitions targetNamespace="http://localhost:8080/axis/HelloClient.jws" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://localhost:8080/axis/HelloClient.jws" xmlns:intf="http://localhost:8080/axis/HelloClient.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="getNameRequest">
  <wsdl:part name="name" type="xsd:string" /> 
  </wsdl:message>
- <wsdl:message name="getNameResponse">
  <wsdl:part name="getNameReturn" type="xsd:string" /> 
  </wsdl:message>
- <wsdl:portType name="HelloClient">
- <wsdl:operation name="getName" parameterOrder="name">
  <wsdl:input message="impl:getNameRequest" name="getNameRequest" /> 
  <wsdl:output message="impl:getNameResponse" name="getNameResponse" /> 
  </wsdl:operation>
  </wsdl:portType>
- <wsdl:binding name="HelloClientSoapBinding" type="impl:HelloClient">
  <wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" /> 
- <wsdl:operation name="getName">
  <wsdlsoap:operation soapAction="" /> 
- <wsdl:input name="getNameRequest">
  <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://DefaultNamespace" use="encoded" /> 
  </wsdl:input>
- <wsdl:output name="getNameResponse">
  <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost:8080/axis/HelloClient.jws" use="encoded" /> 
  </wsdl:output>
  </wsdl:operation>
  </wsdl:binding>
- <wsdl:service name="HelloClientService">
- <wsdl:port binding="impl:HelloClientSoapBinding" name="HelloClient">
  <wsdlsoap:address location="http://localhost:8080/axis/HelloClient.jws" /> 
  </wsdl:port>
  </wsdl:service>
  </wsdl:definitions>

wsdl文件的剖析:
<definitions>wsdl的根元素,定义web服务的名称,并声明wsdl其余部分使用的命名空间。
<types>:定义web服务于服务用户之间交换的信息的所有数据类型。XML schema是默认的类型系统。
message:表示web服务于服务用户之间传递的数据的逻辑定义,描述单向消息,包含0或多个消息元素part。 part主要指请求参数或响应返回值。
portType:通过组合message定义的各种请求和响应消息,定义了web服务支持的各项操作的抽象定义。
binding:指定portType定义的操作和消息的具体协议和数据格式。
port:指定与web服务的绑定地址。
service:聚集一组相关的port元素
当输入http://localhost:8080/axis/HelloClient.jws?method=getName时,显示结果:
  <?xml version="1.0" encoding="UTF-8" ?> 
- <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <soapenv:Body>
- <getNameResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <getNameReturn xsi:type="xsd:string">hello null</getNameReturn> 
  </getNameResponse>
  </soapenv:Body>
  </soapenv:Envelope>

在浏览器中输入http://localhost:8080/axis/HelloClient.jws?method=getName&parameter=luokaiyun时,显示的xml结果如下:
<?xml version="1.0" encoding="UTF-8" ?> 
- <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <soapenv:Body>
- <getNameResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <getNameReturn xsi:type="xsd:string">hello luokaiyun</getNameReturn> 
  </getNameResponse>
  </soapenv:Body>
  </soapenv:Envelope>

在axis的web-inf目录下还有一个server-config.wsdd文件,服务部署描述,该文件一开始没有,可以自行添加如下:
<?xml version="1.0" encoding="UTF-8"?>
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
 <globalConfiguration>
  <parameter name="sendMultiRefs" value="true"/>
  <parameter name="disablePrettyXML" value="true"/>
  <parameter name="adminPassword" value="admin"/>
  <parameter name="attachments.Directory" value="D:\Tomcat6\webapps\axis\WEB-INF\attachments"/>
  <parameter name="dotNetSoapEncFix" value="true"/>
  <parameter name="enableNamespacePrefixOptimization" value="false"/>
  <parameter name="sendXMLDeclaration" value="true"/>
  <parameter name="sendXsiTypes" value="true"/>
  <parameter name="attachments.implementation" value="org.apache.axis.attachments.AttachmentsImpl"/>
  <requestFlow>
   <handler type="java:org.apache.axis.handlers.JWSHandler">
    <parameter name="scope" value="session"/>
   </handler>
   <handler type="java:org.apache.axis.handlers.JWSHandler">
    <parameter name="scope" value="request"/>
    <parameter name="extension" value=".jwr"/>
   </handler>
  </requestFlow>
 </globalConfiguration>
 <handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/>
 <handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder"/>
 <handler name="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"/>
 <service name="AdminService" provider="java:MSG">
  <parameter name="allowedMethods" value="AdminService"/>
  <parameter name="enableRemoteAdmin" value="false"/>
  <parameter name="className" value="org.apache.axis.utils.Admin"/>
  <namespace>http://xml.apache.org/axis/wsdd/</namespace>
 </service>
 <service name="Version" provider="java:RPC">
  <parameter name="allowedMethods" value="getVersion"/>
  <parameter name="className" value="org.apache.axis.Version"/>
 </service>
 <service name="HelloWorld" provider="java:RPC">
  <parameter name="allowedMethods" value="sayHello"/>
  <parameter name="className" value="HelloWorld"/>
 </service>
 <transport name="http">
  <requestFlow>
   <handler type="URLMapper"/>
   <handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/>
  </requestFlow>
  <parameter name="qs:list" value="org.apache.axis.transport.http.QSListHandler"/>
  <parameter name="qs:wsdl" value="org.apache.axis.transport.http.QSWSDLHandler"/>
  <parameter name="qs.list" value="org.apache.axis.transport.http.QSListHandler"/>
  <parameter name="qs.method" value="org.apache.axis.transport.http.QSMethodHandler"/>
  <parameter name="qs:method" value="org.apache.axis.transport.http.QSMethodHandler"/>
  <parameter name="qs.wsdl" value="org.apache.axis.transport.http.QSWSDLHandler"/>
 </transport>
 <transport name="local">
  <responseFlow>
   <handler type="LocalResponder"/>
  </responseFlow>
 </transport>
</deployment>

所有要部署的web服务在此文件中都有描述,有了这个文件,就可以在浏览器中输入http://localhost:8080/axis/servlet/AxisServlet,以查看可用的服务
本例子显示服务如下:
And now... Some Services
AdminService (wsdl)
AdminService
Version (wsdl)
getVersion
HelloWorld (wsdl)
sayHello

部署完服务后,可直接在浏览器中输入如:http://localhost:8080/axis/services/HelloWorld来访问helloworld服务,结果:
HelloWorld
Hi there, this is an AXIS service!

Perhaps there will be a form for invoking the service here...

你可能感兴趣的:(apache,tomcat,xml,Web,浏览器)