首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

基于axis作webservice

2012-10-20 
基于axis做webservice看了很多webservice的例子,很多没看明白。自己试验了下,觉得这样做是比较简单的,把程

基于axis做webservice
看了很多webservice的例子,很多没看明白。自己试验了下,觉得这样做是比较简单的,把程序发布下(在附件中)。

1、下载axis,在http://ws.apache.org/axis2/download/1_5_1/download.cgi
2、做一个WEB工程,文件目录如下
   axis
   -- WEB-INF
   ---- web.xml
   ---- classes
   ------ server-config.wsdd
   ---- lib
   ------- axis.jar
   ------- common-discovery-0.2.jar
   ------- common-logging-1.0.4.jar
   ------- jaxrpc.jar
   ------- log4j.1.2.8.jar
   ------- saaj.jar
   ------- wsdl4j-1.5.1.jar

3、web.xml配置如下
    <?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web
Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
  <display-name>Apache-Axis</display-name>
   
    <listener>
        <listener-class>org.apache.axis.transport.http.AxisHTTPSessionListener</listener-class>
    </listener>
   
  <servlet>
    <servlet-name>AxisServlet</servlet-name>
    <display-name>Apache-Axis Servlet</display-name>
    <servlet-class>
        org.apache.axis.transport.http.AxisServlet
    </servlet-class>
  </servlet>

  <servlet>
    <servlet-name>AdminServlet</servlet-name>
    <display-name>Axis Admin Servlet</display-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>
    <display-name>SOAPMonitorService</display-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>
        <!-- Default to 5 minute session timeouts -->
        <session-timeout>5</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-config.wsdd 是关于作为webserivce 方法描述

  <?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">  
  <handler type="java:org.apache.axis.handlers.http.URLMapper" name="URLMapper"/>  
    
  <service name="hello" provider="java:RPC">  
    <parameter name="className" value="server.HelloService"/>  
    <parameter name="allowedMethods" value="getHello"/>  
  </service>  
    
  <transport name="http">  
    <requestFlow>  
        <handler type="URLMapper"/>  
    </requestFlow>  
  </transport>  
</deployment>

5、调用端代码

  package client;

import java.net.URL;

import javax.xml.rpc.ServiceException;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;

public class Test {

     public static void main(String[] args) throws Exception {
       //标识WebService的具体路径  
        String endpoint = "http://localhost:8006/services/hello";  
        //创建Service实例  
        Service service = new Service();  
        //通过Service实例创建Call实例  
        Call call = (Call)service.createCall();  
        //将WebService的服务路径加入到Call实例中,并为Call设置服务的位置  
        URL url = new URL(endpoint);  
        call.setTargetEndpointAddress(url);  
        //调用WebService方法  
        call.setOperationName("getHello");  
        //调用WebService传入参数  
        String res = (String)call.invoke(new Object[] {"锅巴"});  
        System.out.println(res);  

     }
}

 
  

热点排行