`

使用Axis开发Web Service程序

 
阅读更多

使用Axis开发Web Service程序
    1、新建一个Web工程,工程名为“AxisTest”。
    2、新建“lib”文件夹,然后把主要JAR包:axis.jar,commons-discovery-0.2.jar,commons-logging-1.0.4.jar,jaxrpc.jar,wsdl4j-1.5.1.jar,saaj.jar;可选包(发布服务及生成客户端程序是要用到的):activation.jar;mail.jar都拷贝到此“lib”文件夹下,并把主要的JAR包添加到工程的classpath中;
    3、配置“web.xml”:
    < ?xml version="1.0" encoding="UTF-8"?>
    < web-app version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    < display-name>Apache-Axis< /display-name>
    < listener>
    < listener-class>org.apache.axis.transport.http.AxisHTTPSessionListener< /listener-class>
    < /listener>
    < servlet>
    < servlet-name>AxisServlet< /servlet-name>
    < servlet-class>
    org.apache.axis.transport.http.AxisServlet
    < /servlet-class>
    < /servlet>
    < servlet>
    < servlet-name>AdminServlet< /servlet-name>
    < servlet-class>
    org.apache.axis.transport.http.AdminServlet
    < /servlet-class>
    < load-on-startup>100< /load-on-startup>
    < /servlet>
    < servlet>
    < servlet-name>SOAPMonitorService< /servlet-name>
    < servlet-class>
    org.apache.axis.monitor.SOAPMonitorService
    < /servlet-class>
    < init-param>
    < param-name>SOAPMonitorPort< /param-name>
    < param-value>5001< /param-value>
    < /init-param>
    < load-on-startup>100< /load-on-startup>
    < /servlet>
    < servlet-mapping>
    < servlet-name>AxisServlet< /servlet-name>
    < url-pattern>/servlet/AxisServlet< /url-pattern>
    < /servlet-mapping>
    < servlet-mapping>
    < servlet-name>AxisServlet< /servlet-name>
    < url-pattern>*.jws< /url-pattern>
    < /servlet-mapping>
    < servlet-mapping>
    < servlet-name>AxisServlet< /servlet-name>
    < url-pattern>/services/*< /url-pattern>
    < /servlet-mapping>
    < servlet-mapping>
    < servlet-name>SOAPMonitorService< /servlet-name>
    < url-pattern>/SOAPMonitor< /url-pattern>
    < /servlet-mapping>
    < !-- uncomment this if you want the admin servlet -->
    < !--
    < servlet-mapping>
    < servlet-name>AdminServlet< /servlet-name>
    < url-pattern>/servlet/AdminServlet< /url-pattern>
    < /servlet-mapping>
    -->
    < session-config>
    < session-timeout>20< /session-timeout>
    < /session-config>
    < !-- currently the W3C havent settled on a media type for WSDL;
    http://www.w3.org/TR/2003/WD-wsdl12-20030303/#ietf-draft
    for now we go with the basic 'it's XML' response -->
    < mime-mapping>
    < extension>wsdl< /extension>
    < mime-type>text/xml< /mime-type>
    < /mime-mapping>
    < mime-mapping>
    < extension>xsd< /extension>
    < mime-type>text/xml< /mime-type>
    < /mime-mapping>
    < welcome-file-list id="WelcomeFileList">
    < welcome-file>index.jsp< /welcome-file>
    < welcome-file>index.html< /welcome-file>
    < welcome-file>index.jws< /welcome-file>
    < /welcome-file-list>
    < /web-app>
    4、编写服务端程序server,SayHello.java,编译server.SayHello.java
    package server;
    public class SayHello
    {
    public String getName(String name)
    {
    return "hello "+name;
    }
    }
    5、编写wsdd文件
    deploy.wsdd文件内容如下:
    < deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java=
    "http://xml.apache.org/axis/wsdd/providers/java">
    < service name="SayHello" provider="java:RPC">
    < parameter name="className" value="server.SayHello.getName"/>
    < parameter name="allowedMethods" value="*"/>
    < parameter name="scope" value="session"/>< !-- request, session, or application -->
    < /service>
    < /deployment>
    6、把工程发布到Tomcat并启动Tomcat
    7、发布服务
    编辑一个deploy.bat,Axis_Lib为axis.jar路径。内容如下:
    set Axis_Lib=.\lib
    set Java_Cmd=java -Djava.ext.dirs=%Axis_Lib%
    set Axis_Servlet=http://localhost:8080/AxisTest/servlet/AxisServlet
    %Java_Cmd% org.apache.axis.client.AdminClient -l%Axis_Servlet% deploy.wsdd
    执行这个批处理文件,这时候,如果提示成功的话,访问http://localhost:8080/AxisTest/servlet/AxisServlet或http://localhost:8080/AxisTest/services就会显示服务列表。
    8、生成客户端client stub文件
    在浏览器上访问服务器端的服务,可以下载到WSDL文件,通过Axis的相关工具,可以自动从WSDL文件中生成Web Service的客户端代码。
    编写一个WSDL2Java.bat文件,其内容如下:
    set Axis_Lib=.\lib
    set Java_Cmd=java -Djava.ext.dirs=%Axis_Lib%
    set Output_Path=.\src
    set Package=server.com
    set wsdl_path=http://localhost:8080/AxisTest/services/ SayHello?wsdl
    %Java_Cmd% org.apache.axis.wsdl.WSDL2Java -o%Output_Path% -p%Package% %wsdl_path%
    执行这个批处理文件就可以生成client stub。
    生成的stub client文件列表为:SayHello.java,SayHelloService.java,SayHelloServiceLocator.java,SayHelloSoapBindingStub.java。
    9、编写客户端程序,编译并执行
    1)、Stubs方式
    下面是一段junit测试客户端代码。
    import java.net.URL;
    import junit.framework.Test;
    import junit.framework.TestCase;
    import junit.framework.TestSuite;
    public class TestWSClient extends TestCase {
    public TestWSClient(String string) {
    super(string);
    }
    public void SayHelloClient() throws Exception {
    SayHelloService service = new SayHelloServiceLocator();
    SayHello_PortType client = service.getSayHello() ;
    String retValue = client.getName("clientname");
    System.out.println(retValue);
    }
    public static Test suite() {
    TestSuite suite = new TestSuite();
    suite.addTest(new TestWSClient("SayHelloClient"));
    return suite;
    }
    }
    2)、动态调用方式:
    try {
    // Options options = new Options(args);
    String endpointURL = "http://localhost:8080/AxisTest/services/SayHello";
    Service  service = new Service();
    Call     call    = (Call) service.createCall();
    call.setTargetEndpointAddress( new java.net.URL(endpointURL) );
    call.setOperationName( new QName("SayHello", "getName") );
    String res = (String) call.invoke( new Object[] {"Jack"} );
    System.out.println( res );
    } catch (Exception e) {
    System.err.println(e.toString());
    }

分享到:
评论

相关推荐

    axis开发web service程序

    web service在web接口系统中是jms中很不错的一个选择。而axis又是开发web service的不二人选。本文通过理论结合实例的方式讲解如何通过axis来开发web service。所有实例代码均可copy运行成功。

    Axis开发Web Service程序教程

    本文介绍使用AXIS作为开发环境来体会Web服务的开发过程。

    axis开发web_service程序_学习笔记

    axis开发web_service程序_学习笔记,学习axis开发,学习笔记

    axis开发资料

    项目收集的axis的相关资料~~ Axis.pdf AXIS(Java+WebSerivce)全攻略.mht axis开发.doc 用Axis开发基于Java的Web服务.doc AXIS实现Web服务深入篇.TXT AXIS学习笔记.txt ...使用Axis开发Web Service程序.txt

    基于WAS CE和Axis2开发Web Service应用

    随着Web Service技术迅速发展,基于Web Service开发的应用被使用...本文将介绍如何使用WAS CE(WebSphere Application Server Community Edition)和Apache Axis2开发、部署及测试一个简单的Web Service应用-网上花店。

    用AXIS2开发WebService

    1. AXIS2简介 本文介绍如何在eclipse3.2下用Axis2开发web service,并将Axis2与自己的web service部署在Tomcat 6.0.18下,再通过在C#下写测试程序,来访问Tomcat下的web service。

    Web Service通用客户端和测试工具

    wsCaller是使用Java语言编写的Web Service通用客户端和测试工具。wsCaller可执行程序的发布方式为一个wsCaller.jar包,不包含Java运行环境。你可以把wsCaller.jar复制到任何安装了Java运行环境(要求安装JRE/JDK ...

    Web_Service开发指南电子版 PDF

    WebSevice 让一个程序可以透明地调用互联网程序,不用管具体的实现细节。只要WebService公开了服务接口,远程客户端就可以调用服务。WebService 是基于Http协议的组件服务,WebService是分散式应用程序的发展趋势。 ...

    web技术开发

    该文档基于axis2框架的web service开发,文档中讲述了其开发环境的搭建及一个开发实例程序

    08内存及存储管理(下)

    使用Axis来开发Web services 需要准备 web 服务器,Axis API。本文使用的Web container 是Tomcat5.5, Axis API 采用版本2。 1.1软件下载准备 Tomcat下载地址:http://tomcat.apache.org/download-55.cgi#5.5.20 ...

    Java6开发WebService入门

    之前常常用CXF、Axis2、XFire等来开发结合Java语言来开发Web Service应用,这样的好处是用途广,灵活,另外一个重要原因是我们的生产环境是Java5。 但实际上Java6中已经支持用Java开发WebService应用了,而且很方便...

    关于电信最新isag和ismp的详细实例说明

    本文档详细介绍了电信最新的ISAG和ISMP的程序开发,及axis的配置等.

Global site tag (gtag.js) - Google Analytics