1.首先使用IDE建立一个WEB 工程。
2.把需要的.jar 包文件导入到工程中。
3.在web.xml 文件中加入 axis 的servlet 配置。文件如下:
<?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>Edu Application</display-name> <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>*.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> <welcome-file>index.htm</welcome-file> </welcome-file-list> </web-app>
4.COPY server-config.wsdd 和 deploy.wsdd 两个文件到 WEB-INF目录下 ,这里为了方便只列出 deploy.wsdd文件的内容,因为 server-config.wsdd 文件可以搜索到。
<?xml version="1.0" encoding="gb2312"?> <deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <globalConfiguration> <parameter name="adminPassword" value="admin"/> <parameter name="attachments.implementation" value="org.apache.axis.attachments.AttachmentsImpl"/> <parameter name="sendXsiTypes" value="true"/> <parameter name="sendMultiRefs" value="true"/> <parameter name="sendXMLDeclaration" value="true"/> <parameter name="axis.sendMinimizedElements" value="true"/> <requestFlow> <handler type="java:org.apache.axis.handlers.JWSHandler"> <parameter name="scope" value="session"/> </handler> <handler type="java:org.apache.axis.handlers.JWSHandler"> <parameter name="scope" value="request"/> <parameter name="extension" value=".jwr"/> </handler> </requestFlow> </globalConfiguration> <handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder"/> <handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/> <handler name="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"/>
<!-- 关键的配置在这里,相信一看就明白了。-->
<service name="SayHello" provider="java:RPC"> <!-- 服务类名 --> <parameter name="className" value="server.SayHello"/> <!-- 允许访问所有方法 --> <parameter name="allowedMethods" value="*"/> </service> <transport name="http"> <requestFlow> <handler type="URLMapper"/> <handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/> </requestFlow> </transport> <transport name="local"> <responseFlow> <handler type="LocalResponder"/> </responseFlow> </transport> </deployment> 5. 编写 SayHello.java
package server; public class SayHello { public String getName(String name) { return "hello "+name; } public String toUpper(String str) { return str.toUpperCase(); } public Integer sum(Integer a,Integer b) { return new Integer(a.intValue()+b.intValue()); } }
编写HelloClient.java
import javax.xml.namespace.QName;
import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory;
public class HelloClient { protected final Log log = LogFactory.getLog(this.getClass()); public String getName(String name) { return "hello "+name; } public static void main(String[] args) { HelloClient hc = new HelloClient(); String str = (String)hc.sendXml("getName",new Object[]{"孟昭熠"}); System.out.println("str=" + str); Integer sum = (Integer) hc.sendXml("sum",new Object[]{new Integer(2),new Integer(3)}); System.out.println(sum.intValue()); }
public Object sendXml(String method, Object[] objs) { String endPoint = "http://127.0.0.1:88/Axis/services/SayHello"; try { Service service = new Service(); Call call = (Call) service.createCall(); call.setOperationName(new QName(endPoint, method)); call.setTargetEndpointAddress(endPoint); Object o = call.invoke(objs); return o; } catch (Exception e) { log.error("ex=" + e.toString()); return null; } } } 6.部署WEB 应用后,访问你的应用 ,例如:http://127.0.0.1:88/Axis/services
可以看到相关的方法名称已经列出来了。
7.运行HelloClient 程序,可以得到结果。