軟件環(huán)境
1、?
Windows 2000 professional sp4
2、?
axis-1_3 (從http://ws.apache.org/axis/下載);
3、?
tomcat-5.0.28;
4、?
jdk1.4.2
Axis的測試demo
將axis-bin-1_3.zip解壓的webapps目錄下的axis目錄拷貝到%TOMCAT_HOME%\webapps目錄下(最好在%TOMCAT_HOME%\webapps\axis\WEB-INF\lib增加:activation.jar、mail.jar和tools.jar),然后啟動tomcat,在瀏覽器地址欄輸入http://localhost:8080/axis/就可以看到axis的主頁面了。
Webservice程序發(fā)布
1、編寫簡單的webservice程序如下:
package test.webservice;
?
public class Calculator {
??? public int add(int i1, int i2) {
??????? return i1 + i2;
??? }
?
??? public int subtract(int i1, int i2) {
??????? return i1 - i2;
??? }
?
??? public String show(String arg) {
??????? return arg;
??? }
}
|
1、
將程序包拷貝到%TOMCAT_HOME%\webapps\axis\WEB-INF\classes目錄下;
2、
配置環(huán)境變量
AXIS_HOME?。健xis-bin-1_3.zip的解壓路徑
AXISCLASSPATH?。健?AXIS_HOME%\lib\axis.jar;%AXIS_HOME%\lib\axis-schema.jar;%AXIS_HOME%\lib\commons-discovery-0.2.jar;%AXIS_HOME%\lib\commons-logging-1.0.4.jar;%AXIS_HOME%\lib\jaxrpc.jar;%AXIS_HOME%\lib\log4j-1.2.8.jar;%AXIS_HOME%\lib\saaj.jar;%AXIS_HOME%\lib\wsdl4j-1.5.1.jar;
3、
編寫發(fā)布文件deploy.wsdd(該文件的模板可以從%AXIS_HOME%\samples\stock目錄下找到),將其放到%TOMCAT_HOME%\webapps\axis\WEB-INF目錄下;
<deployment name="test" xmlns="http://xml.apache.org/axis/wsdd/"
??? xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
?
? <service name="Calculator" provider="java:RPC">
?
? <!--指定作為webservice的類名-->
??? <parameter name="className" value="test.webservice.Calculator" />
??? <!--指定可以被調(diào)用的方法,有選擇使用逗號間隔,全部使用*-->
??? <parameter name="allowedMethods" value="add,subtract,show" />???
? </service>
?
</deployment>
|
4、
命令行方式轉(zhuǎn)到%TOMCAT_HOME%\webapps\axis\WEB-INF目錄下,使用命令:
java -cp %AXISCLASSPATH% org.apache.axis.client.AdminClient -lhttp://localhost/axis/services/AdminService deploy.wsdd
|
將deploy.wsdd文件轉(zhuǎn)變?yōu)閟erver-config.wsdd(其實同時還生成attachments文件夾),這樣在tomcat啟動時可以被AxisServlet調(diào)用初始化。注意:使用命令時tomcat應(yīng)處于啟動狀態(tài)。
5、
發(fā)布完畢,在瀏覽器地址欄輸入http://localhost:8080/axis/services可以看到你發(fā)布的webservice了,可以查看具體的wsdl。
6、
java客戶端測試程序
package test.webservice;
?
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.encoding.XMLType;
?
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
?
public class CalcClient {
??? public static void main(String[] args) throws Exception {
??????? String endpoint = "http://localhost:8080/axis/services/Calculator";
?
??????? Service service = new Service();
??????? Call call = (Call) service.createCall();
?
??????? call.setTargetEndpointAddress(new java.net.URL(endpoint));
??????? // call.setOperationName("add");
??????? call.setOperationName("subtract");
??????? // call.setOperationName("show");
?
??????? call.addParameter("p1", XMLType.XSD_INT, ParameterMode.IN);
??????? call.addParameter("p2", XMLType.XSD_INT, ParameterMode.IN);
??????? call.setReturnType(XMLType.XSD_INT);
?
??????? Integer ret = (Integer) call.invoke(new Object[] { new Integer(5), new Integer(3) });
??????? // String ret = (String) call.invoke(new Object[] { "axis你好!" });
?
??????? System.out.println("The value of webservice return is :" + ret);
??? }
}
|
?
添加用戶驗證
用戶驗證使用axis的簡單用戶驗證handler:SimpleAuthenticationHandler。
1、
在deploy.wsdd文件中增加以下紅色字體部分
<deployment name="test" xmlns="http://xml.apache.org/axis/wsdd/"
??? xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
?
? <service name="Calculator" provider="java:RPC">
?
? <!--指定作為webservice的類名-->
??? <parameter name="className" value="test.webservice.Calculator" />
??? <!--指定可以被調(diào)用的方法,有選擇使用逗號間隔,全部使用*-->
??? <parameter name="allowedMethods" value="add,subtract,show" />
??
?<!--指定可以調(diào)用用戶-->
??? <parameter name="allowedRoles" value="testname" />????
??? <requestFlow>?????
???
??? <handler type="Authenticate"/>
???
?</requestFlow>
? </service>
?
</deployment>
|
2、
在%TOMCAT_HOME%\webapps\axis\WEB-INF目錄下建立users.lst文件,內(nèi)容如下:
3、
重復(fù)
Webservice
程序發(fā)布的第4步,重新發(fā)布(也可以直接修改
server-config.wsdd文件
)。
4、
修改客戶端調(diào)用程序
package test.webservice;
?
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.encoding.XMLType;
?
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
?
public class CalcClient {
??? public static void main(String[] args) throws Exception {
??????? String endpoint = "http://localhost:8080/axis/services/Calculator";
?
??????? Service service = new Service();
??????? Call call = (Call) service.createCall();
???????
??????? //
用戶驗證
???????
call.getMessageContext().setUsername("testname"); //
用戶名
??????? call.getMessageContext().setPassword("testpass"); //
密碼
?
??????? call.setTargetEndpointAddress(new java.net.URL(endpoint));
??????? // call.setOperationName("add");
??????? call.setOperationName("subtract");
??????? // call.setOperationName("show");
?
??????? call.addParameter("p1", XMLType.XSD_INT, ParameterMode.IN);
??????? call.addParameter("p2", XMLType.XSD_INT, ParameterMode.IN);
??????? call.setReturnType(XMLType.XSD_INT);
?
??????? Integer ret = (Integer) call.invoke(new Object[] { new Integer(5), new Integer(3) });
??????? // String ret = (String) call.invoke(new Object[] { "axis
你好!
" });
?
??????? System.out.println("The value of webservice return is :" + ret);
??? }
}
|
?
本例程在上述軟件環(huán)境上測試通過,源碼下載:
axisdemo
?
posted on 2006-05-06 20:31
野草 閱讀(3545)
評論(3) 編輯 收藏 所屬分類:
axis