.建立WEB工程 testservice
目錄如下:
cn--test--service--impl
2.要匯入的包
activation-1.0.2.jar
commons-codec-1.3.jar
commons-httpclient-3.0.jar
commons-logging-1.0.4.jar
jaxen-1.1-beta-8.jar
jdom-1.0.jar
log4j-1.2.x.jar
mail-1.3.3_01.jar
spring-1.2.x.jar
stax-api-1.0.jar
wsdl4j-1.5.2.jar
wstx-asl-2.9.jar
xbean-2.1.0.jar
xbean-spring-2.2.jar
xfire-all-1.0.jar
XmlSchema-1.0.jar
3.資料發布端
(1)編寫發布介面
package cn.test.service;
public interface ADService {
String[][] getDatas();
}
(2)編寫實作類別
package cn.test.service.impl;
import cn.test.service.ADService;
public class ADServiceImpl implements ADService{
public String[][] getDatas() {
String ads[][] = new String[3][3];
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
ads[i][j] = String.valueOf(i * j);
}
}
return ads;
}
}
4.增加xfire的設定檔services.xml
在src目錄中加入/src/META-INF/xfire/services.xml
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<name>AD</name> <!-- 這個名字比較重要,調用時用的就是它 -->
<namespace>http://localhost/ADService</namespace>//使用這個地址訪問
<serviceClass>cn.test.service.ADService</serviceClass>
<implementationClass>
cn.test.service.impl.ADServiceImpl //實現的類
</implementationClass>
<!-- 這個東西是一個修飾器 貌似調用了SPRING的整合有關 加了會報一個錯誤
<inHandlers>
<handler
handlerClass="org.codehaus.xfire.demo.handlers.CheckVersionHandler" />
</inHandlers>
-->
</service>
</beans>
5.配置web.xml
在web.xml中加入:
<servlet>
<servlet-name>XFireServlet</servlet-name> //使用內建的Servlet
<display-name>XFire Servlet</display-name>
<servlet-class>
org.codehaus.xfire.transport.http.XFireConfigurableServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>XFireServlet</servlet-name>
<url-pattern>/services/*</url-pattern> //過濾的路徑
</servlet-mapping>
6.測試xfire是否部署成功
http://localhost/testservice/services/AD?wsdl
7.編寫用戶端
package cn.test.service;
import java.net.MalformedURLException;
import org.codehaus.xfire.XFire;
import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
public class CallService {
public String[][] getData(){
Service serviceModel = new ObjectServiceFactory().create(ADService.class); //注意,此處用的介面是發布端的介面ADService.class
XFire xfire = XFireFactory.newInstance().getXFire();
XFireProxyFactory factory = new XFireProxyFactory(xfire);
String serviceUrl = "http://localhost/testservice/services/AD";
ADService client = null;
try {
client = (ADService) factory.create(serviceModel, serviceUrl); //通過類名和地址來達到類的執行個體
} catch (MalformedURLException e) {
System.out.println("WsClient.callWebService(): EXCEPTION: " + e.toString());
}
String[][] datas = null ;
try {
datas = client.getDatas();
} catch (Exception e){
System.out.println("WsClient.callWebService(): EXCEPTION: " + e.toString());
}
//將所有的資料逐個列印出來
int len = datas.length;
for(int i=0;i<len;i++){
int len2 = datas[i].length;
for(int j=0;j<len2;j++){
System.out.print("\t"+datas[i][j]);
}
System.out.println();
}
return datas;
}
}
8.寫一個JSP調用用戶端
<%@ page language="java" import="java.util.*,cn.yicha.service.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
webservice test!
<%
CallService cs = new CallService();
cs.getData();
%>
</body>
</html>