Axis实现
Axis server实现
步骤一 创建Web工程
步骤二 下载Axis包
步骤三 将Axis包加载到创建的Web工程(将Axis->lib文件夹下的包复制到Web工程->WebRoot->WEB-INF->lib文件夹下)
activation-1.1.jar axis.jar axis-ant.jar commons-discovery-0.2.jar commons-logging-1.0.4.jar jaxrpc.jar log4j-1.2.8.jar mail-1.4.jar saaj.jar wsdl4j-1.5.1.jar
步骤四 修改Web工程->WebRoot->WEB-INF->web.xml
在web.xml文件<web-app></web-app>中加上
<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>
<!-- 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>
<!-- ****************** axis ws end ****************** -->
步骤五 在Web工程下创建一个类并在类中写一个方法
例:
package com.soap.server;
public class HelloAxis
{
public String sayHello(String name)
{
String info = "my name is:"+ name;
return info;
}
}
步骤六 编写wsdd文件
例:
<?xml version="1.0" encoding="UTF-8"?>
<deployment name="defaultClientConfig"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"
xmlns:handler="http://xml.apache.org/axis/wsdd/providers/handler" xmlns="http://xml.apache.org/axis/wsdd/">
<globalConfiguration name="defaultClientConfig">
<requestFlow name="RequestFlow1" type="">
<handler name="Handler1" type="java:org.apache.axis.handlers.JWSHandler">
<parameter name="scope" value="session"/>
</handler>
<handler name="Handler2" type="java:org.apache.axis.handlers.JWSHandler">
<parameter name="scope" value="request"/>
<parameter name="extension" value=".jwr"/>
</handler>
</requestFlow>
</globalConfiguration>
<handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/>
<handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder"/>
<handler name="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"/>
<transport name="http" type="">
<requestFlow name="RequestFlow1" type="">
<handler name="Handler1" type="URLMapper"/>
<handler name="Handler2" type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/>
</requestFlow>
</transport>
<transport name="local" type="">
<responseFlow name="ResponseFlow1" type="">
<handler name="Handler1" type="LocalResponder"/>
</responseFlow>
</transport>
<!--这里配置了一个Web Service,如果有多个Web Service,就按这种格式在下面增加即可-->
<service name="HelloAxis" provider="java:RPC">
<parameter name="scope" value="Request"/>
<parameter name="className" value="com.soap.server.HelloAxis"/>
<parameter name="allowedMethods" value="*"/>
<!--定义操作(这里相当于将我们步骤五中创建的类中的sayhello方法暴露给别人,供其访问)
<operation name="sayHello" qname="operNS:sayHello" xmlns:operNS="http://tempuri.org/ns.xsd"
returnQName="returnStatus" returnType="rtns:string"
xmlns:rtns="http://www.w3.org/2001/XMLSchema" soapAction="" >
<!--定义所传的参数-->
<parameter qname="name" type="tns:string" xmlns:tns="http://www.w3.org/2001/XMLSchema"/>
</operation>
</service>
</deployment>
发布我们新建的Web工程
打开浏览器访问:http://ip:port/projectname/services/HelloAxis
Axis client实现
Axis client实现
步骤一 创建Web工程
步骤二 下载Axis包
步骤三 将Axis包加载到创建的Web工程(将Axis->lib文件夹下的包复制到Web工程->WebRoot->WEB-INF->lib文件夹下)
步骤四 在Web工程下创建一个类并在类中提供main方法
例:
package com.soap.client;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
import javax.xml.soap.SOAPException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class ServiceClient
{
public static void main(String[] args) throws ServiceException, MalformedURLException, RemoteException, SOAPException
{
//发布的soap service地址
String soapPath = "http://ip:port/projectname/services/HelloAxis";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(soapPath));
call.setOperationName("sayHello");
String response = (String) call.invoke(new Object[]{"张三"});
System.out.println(response);
}
}