webservice 快速 搭建 简单 实例 axis版

lvzhiliang 2014-11-26

1、服务器端

1.1、创建一个Web工程WS_Test

1.2、把axis相关jar包考入工程的lib文件夹下(jar包见附件)

1.3、配置web.xml,内容如下

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEweb-appPUBLIC"-//SunMicrosystems,Inc.//DTDWeb

Application2.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-AxisServlet</display-name>

<servlet-class>

org.apache.axis.transport.http.AxisServlet

</servlet-class>

</servlet>

<servlet>

<servlet-name>AdminServlet</servlet-name>

<display-name>AxisAdminServlet</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>

<!--uncommentthisifyouwanttheadminservlet-->

<!--

<servlet-mapping>

<servlet-name>AdminServlet</servlet-name>

<url-pattern>/servlet/AdminServlet</url-pattern>

</servlet-mapping>

-->

<session-config>

<!--Defaultto5minutesessiontimeouts-->

<session-timeout>5</session-timeout>

</session-config>

<!--currentlytheW3ChaventsettledonamediatypeforWSDL;

http://www.w3.org/TR/2003/WD-wsdl12-20030303/#ietf-draft

fornowwegowiththebasic'it'sXML'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-listid="WelcomeFileList">

<welcome-file>index.jsp</welcome-file>

<welcome-file>index.html</welcome-file>

<welcome-file>index.jws</welcome-file>

</welcome-file-list>

</web-app>

1.4、在src下创建对外提供调用的service类test_1.Ws_1.java

内容如下:

packagetest_1;

publicclassWs_1{

publicStringfunction_1(Stringstr_1){

System.out.println("***service被调用了***接收参数:"+str_1);

Stringstr="scorpio:"+str_1+"isok";//接到客户端请求,然后处理

//……

returnstr;//最后把结果返回客户端

}

}

1.5、修改tomcat下的server.xml

本人server.xml文件路径为D:/apache-tomcat-5.5.26/conf/server.xml

在</Host>上一行增加或修改为

<Contextpath='/WS_Test'docBase='D:/workspace/WS_Test/WebRoot'debuge='0'reloadable='true'/>

其中:/WS_Test为上下文的根,在这里用工程名命名(可随意)

D:/workspace/WS_Test/WebRoot为工程web所在的路径

1.6、在WEB-INF下创建deploy.wsdd文件,内容如下

<deploymentxmlns="http://xml.apache.org/axis/wsdd/"xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">

<servicename="Say_Hello_Service"provider="java:RPC">

<parametername="className"value="test_1.Ws_1"/>

<parametername="allowedMethods"value="*"/>

</service>

</deployment>

注:Say_Hello_Service,为service的名字

test_1.Ws_1,为Say_Hello_Service对应的类

1.7、在WEB-INF下创建deploy.bat文件,内容如下

setAxis_Lib=D:/workspace/WS_Test/WebRoot/WEB-INF/lib

setJava_Cmd=java-Djava.ext.dirs=%Axis_Lib%

setAxis_Servlet=http://localhost:7777/WS_Test/servlet/AxisServlet

%Java_Cmd%org.apache.axis.client.AdminClient-l%Axis_Servlet%deploy.wsdd

pause

其中D:/workspace/WS_Test/WebRoot/WEB-INF/lib对应改为自己的工程lib所在的路径

http://localhost:7777/WS_Test/servlet/AxisServlet中的7777改为自己的端口号

注:这里特别注意,在tomcat中一定要配置工程上下文的根(我这里用的是工程名:WS_Test)

1.8、先启动tomcat,然后运行刚创建的deploy.bat

执行完毕后,WEB-INF下会多出一个server-config.wsdd文件

1.9、在浏览器地址栏里输入http://localhost:7777/WS_Test/servlet/AxisServlet(7777改成自己tomcat的端口号)

如有如下内容则服务器端编码完毕

•Say_Hello_Service(wsdl)

◦function_1

2、客户端

2.1、建立一个java工程WS_Test_client

2.2、工程下文件夹WS_Test_client下建立一个lib文件夹

把axis相关jar包考入工程的lib文件夹下(jar包见附件)

2.3、工程文件夹WS_Test_client下建立一个test_1_client文件夹

2.4、工程文件夹WS_Test_client下直接创建wsdl2java.bat文件

内容如下

setAxis_Lib=D:/workspace/WS_Test_client/lib

setJava_Cmd=java-Djava.ext.dirs=%Axis_Lib%

setOutput_Path=D:/workspace/WS_Test_client

setPackage=test_1_client

%Java_Cmd%org.apache.axis.wsdl.WSDL2Java–p%Package%-o%Output_Path%http://localhost:7777/WS_Test/services/Say_Hello_Service?wsdl

Pause

其中D:/workspace/WS_Test_client/lib为客户端java工程lib路径

D:/workspace/WS_Test_client/test_1_client存放生成文件路径

test_1_client生成的类的包名

2.5运行wsdl2java.bat文件

前提:服务器端服务已开启

运行完毕后会多出

Say_Hello_ServiceSoapBindingStub.java

Ws_1.java

Ws_1Service.java

Ws_1ServiceLocator.java

等文件

2.6、创建调用服务器service的类test_1_client.Main.java

packagetest_1_client;

publicclassMain{

/**

*@paramargs

*/

publicstaticvoidmain(String[]args){

//TODOAuto-generatedmethodstub

Ws_1ServiceLocatorlocal=newWs_1ServiceLocator();

try{

Ws_1ws=local.getSay_Hello_Service();

Stringstr=ws.function_1("heihei");//调用服务器端方法

System.out.println(str);

}catch(Exceptione){

e.printStackTrace();

}

}

}

2.7、运行test_1_client.Main.java输出台输出服务器端返回结果即调用服务器端成功

相关推荐