::Z::Thinking::

::Simple::
文章 - 124,收藏 - , 评论 - 49, trackbacks - 0
Web服务部署内幕[绝对原创]
http://www.chinaunix.net 作者:simbasun  发表于:2003-09-27 23:06:35

[b:6c3eda4bd1]Web服务部署内幕[/b:6c3eda4bd1]

(作者:simbasun 转载请注明出处)


1.第一口就吃到馅,配置一个Web服务的最简方法
===============================================================================================

为了能让web服务先跑起来,先给出一个Web服务的原型,以便于后面的讨论。
我们从一个最简单的例子开始,只给出必须的东西。

所需软件:
1.Tomcat4.1.2
2.一个Java编译器,jdk或JBuilder等等,这是为了编译我们的Java源程序,于web服务无关。

所需文件:
1.sayHello.java
2.web.xml
3.server-config.xml
4.Java Packages: axis.jar,jaxrpc.jar,tt-bytecode.jar,wsdl4j.jar,xercesImpl.jar,xml-apis.jar

至于Tomcat怎么安装我就不说了,网上关于Tomcat安装的文章有很多。
这六个package,从ibm和apache的网站上都可以下得到。

只需要这些,我们就可以部署自己的Web服务了。。
下面是目录结构:
webapps/test/WEB-INF/web.xml
webapps/test/WEB-INF/server-config.wsdd
webapps/test/WEB-INF/classes/sayHello.class
webapps/test/WEB-INF/lib/xxx.jar ---所需得六个packages


web.xml
---------------------------------------------

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

<web-app>

<servlet>
    <servlet-name>Axis</servlet-name>
    <!--实际servlet程序,这里是AxisServlet-->
    <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
</servlet>

<!-- ### 定义servlet和url的对应关系-->

<servlet-mapping>
    <servlet-name>Axis</servlet-name>
    <url-pattern>/services/*</url-pattern>
</servlet-mapping>

</web-app>

---------------------------------------------

server-config.wsdd
---------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<deployment xmlns:java="http://xml.apache.org/axis/wsdd/providers/java" xmlns="http://xml.apache.org/axis/wsdd/">

<handler type="java:org.apache.axis.handlers.http.URLMapper" name="URLMapper"/>
  
<service name="sayHelloService" provider="java:RPC">
  <parameter name="className" value="sayHello"/>
  <parameter name="allowedMethods" value="sayHelloTo"/>
</service>

<transport name="http">
  <requestFlow>
    <handler type="URLMapper"/>
  </requestFlow>
</transport>

</deployment>
---------------------------------------------

sayHello.java
---------------------------------------------
public class sayHello
{
  public String sayHelloTo(String aname)
  {
    return "How are you, " + aname;
  }
}
---------------------------------------------


假设ip地址192.168.0.1,端口号是80,我们输入下面的url得到服务列表(当然这里只有一个):
http://192.168.0.1/test/services
如果你的端口号是8080,就应该输入http://192.168.0.1:8080/test/services,后面同理。

浏览器显示:
——————————————
|And now... Some Services |
| sayHelloService (wsdl)  |
|  .sayHelloTo            |   
——————————————

sayHelloService是我们的服务名,右侧的 (wsdl)是一个链接指向sayHelloService的WSDL文档,
这个文档是由Axis自动生成的。
sayHelloTo当然就是我们的方法了。。。

点击(wsdl)链接或输入下面的url,得到WSDL:
http://192.168.0.1/test/services/sayHelloService?wsdl

浏览器显示sayHelloService的WSDL文档:

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://192.168.0.1/test/services/sayHelloService/test/services/sayHelloService" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://192.168.0.1/test/services/sayHelloService/test/services/sayHelloService-impl" xmlns:intf="http://192.168.0.1/test/services/sayHelloService/test/services/sayHelloService" 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:message name="sayHelloToResponse">
    <wsdl:part name="return" type="xsd:string"/>
  </wsdl:message>
  <wsdl:message name="sayHelloToRequest">
    <wsdl:part name="aname" type="xsd:string"/>
  </wsdl:message>
  <wsdl:portType name="sayHello">
    <wsdl:operation name="sayHelloTo" parameterOrder="aname">
      <wsdl:input message="intf:sayHelloToRequest" name="sayHelloToRequest"/>
      <wsdl:output message="intf:sayHelloToResponse" name="sayHelloToResponse"/>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="sayHelloServiceSoapBinding" type="intf:sayHello">
    <wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="sayHelloTo">
      <wsdlsoap:operation soapAction=""/>
      <wsdl:input name="sayHelloToRequest">
        <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://192.168.0.1/test/services/sayHelloService/test/services/sayHelloService" use="encoded"/>
      </wsdl:input>
      <wsdl:output name="sayHelloToResponse">
        <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://192.168.0.1/test/services/sayHelloService/test/services/sayHelloService" use="encoded"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="sayHelloService">
    <wsdl:port binding="intf:sayHelloServiceSoapBinding" name="sayHelloService">
      <wsdlsoap:address location="http://192.168.0.1/test/services/sayHelloService"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>



我们甚至不用客户端,就可以查看服务是否部署成功以及获得返回结果
用Get方法获得soap流,我们要用下面的url:
(真正调用Web服务,用的是Post方法,这个后面会讲)

http://192.168.0.1/test/services/sayHelloService?method=sayHelloTo&aname=everybody

浏览器显示的是乱码,我们点右键查看源文件,结果如下:

<p>Got response message</p>
<?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>
  <sayHelloToResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
   <sayHelloToReturn xsi:type="xsd:string">How are you, everybody</sayHelloToReturn>
  </sayHelloToResponse>
 </soapenv:Body>
</soapenv:Envelope>

这就是我们想要的结果吗?这只是服务器端送回来的SOAP消息,不过我们想要的结果在里面。。。


为了真正调用我们的Web服务,下面给出一个Client:

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;

  public class test
  {
    public static void main(String [] args)
    {
      try {
             String endpoint = "http://192.168.0.1/test/services/sayHelloService";
             Service  service = new Service();
             Call     call    = (Call) service.createCall();
             call.setTargetEndpointAddress( new java.net.URL(endpoint) );
             call.setOperationName(new QName("http://sayHelloService", "sayHelloTo"));
             String ret = (String) call.invoke( new Object[] { args[0] } );
             System.out.println(ret);


Trackback: http://tb.donews.net/TrackBack.aspx?PostId=61432


[点击此处收藏本文]  发表于2004年08月06日 5:00 PM




正在读取评论……