服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

web服务部署内幕


  web服务部署内幕

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="javarg.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/" xmlnssd="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">
<wsdlperation name="sayhelloto" parameterorder="aname">
<wsdl:input message="intf:sayhellotorequest" name="sayhellotorequest"/>
<wsdlutput message="intf:sayhellotoresponse" name="sayhellotoresponse"/>
</wsdlperation>
</wsdl:porttype>
<wsdl:binding name="sayhelloservicesoapbinding" type="intf:sayhello">
<wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdlperation name="sayhelloto">
<wsdlsoapperation 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>
<wsdlutput 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"/>
</wsdlutput>
</wsdlperation>
</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/" xmlnssd="http://www.w3.org/2001/xmlschema" xmlnssi="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.settargetendpointadd

扫描关注微信公众号