JSP学习笔记(一百二十三):使用axis1.4开发webservice应用(二)

拷贝自博客园--

魔豆的BLOG

 

 

通过上篇文章,已经可以通过axis1.4发布服务以及调用服务了,不过上篇文章发布服务使用的方法不够灵活,这里介绍一种灵活的方式

一、以Stubs的方式发布服务


1.以这种方式发布还需要两个J2EE包

?
activation.jar
  
mail.jar



2.修改web.xml,我使用了struts2,和axis的配置会有冲突,为了避免冲突,对struts2的相关配置也做了对应修改,这里贴出完整web.xml文件内容:

?
<?xml version= "1.0" encoding= "UTF-8" ?>
<web-app id= "WebApp_ID" version= "2.4"
     <display-name> static </display-name>
     <filter>
         <filter-name>struts2</filter-name>
         <filter- class >org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter- class >
     </filter>
     <filter-mapping>
         <filter-name>struts2</filter-name>
         <url-pattern>*.action</url-pattern>
     </filter-mapping>
     <filter-mapping>
         <filter-name>struts2</filter-name>
         <url-pattern>/struts/dojo/*</url-pattern>
     </filter-mapping> 
  
     <servlet>
         <servlet-name>AxisServlet</servlet-name>
         <servlet- class >org.apache.axis.transport.http.AxisServlet</servlet- class >
     </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>/services/*</url-pattern>
     </servlet-mapping>
  
     <welcome-file-list>
         <welcome-file>index.htm</welcome-file>
     </welcome-file-list>
</web-app>



3.编写服务类 com.site.webservice.MyService.java

?
package com.site.webservice;
  
public class MyService{
     public String getName(String name)
     {
         return "hi," + name;
     }
}



4.在项目WEB-INF目录下创建deploy.wsdd文件,内容为:

?
<deployment xmlns= "http://xml.apache.org/axis/wsdd/"
     <service name= "MyService" provider= "java:RPC" >
         <parameter name= "className" value= "com.site.webservice.MyService" />
         <parameter name= "allowedMethods" value= "*" />
     </service>
</deployment>



5.在项目WEB-INF目录下创建deploy.bat文件,内容为:

?
set Axis_Lib=D:\workspace\proj1\WebContent\WEB-INF\lib
set Java_Cmd=java -Djava.ext.dirs=%Axis_Lib%
set Axis_Servlet=http: //localhost:8080/proj1/servlet/AxisServlet
%Java_Cmd% org.apache.axis.client.AdminClient -l%Axis_Servlet% deploy.wsdd


dos下进入D:\workspace\proj1\WebContent\WEB-INF目录,执行deploy.bat,执行成功后会提示:

?
Processing file deploy.wsdd
<Admin>Done processing</Admin>


通过http://localhost:8080/proj1/services可以检查服务是否发布成功



6.编写客户端代码调用我们创建的服务:

?
Service service = new Service();
Call call = (Call)service.createCall();
call.setOperationName( new QName(endPoint, "getName" ));
call.setTargetEndpointAddress( new URL(endPoint));
String result = (String)call.invoke( new Object[]{ "abcd" });
System.out.println(result);


执行该代码打印内容为:hi,abcd



7.如果需要卸载MyService服务,在WEB-INF目录下创建undeploy.wsdd文件:

?
<undeployment xmlns= "http://xml.apache.org/axis/wsdd/" >  
  <service name= "MyService" />  
</undeployment>


在WEB-INF目录下创建undeploy.bat文件:

?
set Axis_Lib=D:\workspace\proj1\WebContent\WEB-INF\lib
set Java_Cmd=java -Djava.ext.dirs=%Axis_Lib%
set Axis_Servlet=http: //localhost:8080/proj1/servlet/AxisServlet
%Java_Cmd% org.apache.axis.client.AdminClient -l%Axis_Servlet% undeploy.wsdd


dos下进入D:\workspace\proj1\WebContent\WEB-INF目录,执行undeploy.bat,MyService服务就被卸载了


二、以Stubs的方式调用服务

 
1、在WEB-INF目录下创建client.bat文件:

?
set Axis_Lib=D:\workspace\proj1\WebContent\WEB-INF\lib
set Java_Cmd=java -Djava.ext.dirs=%Axis_Lib%
set Output_Path=D:\workspace\proj1\src
set Package=com.site.client
%Java_Cmd% org.apache.axis.wsdl.WSDL2Java -o%Output_Path% -p%Package% http: //localhost:8080/proj1/services/MyService?wsdl


dos下进入D:\workspace\proj1\WebContent\WEB-INF目录,执行client.bat,会在com.site.client下自动生成如下文件类:

MyServiceSoapBindingStub.java

SayHello.java

SayHelloService.java

SayHelloServiceLocator.java


2、客户端调用

?
SayHelloService service = new SayHelloServiceLocator();
SayHello client = service.getMyService() ;
String result = client.getName( "abcdefg" );
System.out.println(result);

你可能感兴趣的:(webservice)