經過了兩天多的摸索,axis終于配置成功了。步驟其實很簡單的,主要是關于classpath的配置。axis主頁上安裝向導其實寫的明白了,鑒于研究Web Service的人都是有一定java基礎的人,所以上面并沒有詳細說明關于classpath的設置。像我這樣的java新手自然要走很多彎路,沒辦法的事情,這是學習的一個過程。
axis的配置很簡單,過程如下
1. 將axis-src-1_2_1這個包解壓后,將axis-1_2_1\webapps下axis的所有東西拷貝到你剛剛建立的TOMCAT的webapps下。目錄結構如下:
然后查看一下你的axis的WEB-INF的lib下*.jar文件是否全,應該有8個
axis.jar
axis-ant.jar
commons-discovery-0.2.jar
commons-logging-1.0.4.jar
jaxrpc.jar
log4j-1.2.8.jar
saaj.jar
wsdl4j-1.5.1.jar
(一定看好各個文件的文件名,一會將配置CLASSPATH)
axis的安裝向導還要求其他的幾個.jar文件,包括xerces.jar,和mail.jar.這些都可以在Apache網站上找到/
2. 然后到axis-1_2_1目錄下,找到lib文件夾,拷貝其中的activation.jar文件,到
Tomcat\webapps\axis\WEB-INF\lib,和上面的8個jar文件在一起,就OK了。
3.剩下的就是整個過程中最為關鍵的部分,如果弄不好你需要走很彎路的。
關于classpath?的設置,包括兩部分。一. 有關Tomcat的classpath的設置,這一部分是最容易被忽視的,很多關于axis的安裝的文章都沒有講。這就使你在后面遇到很多問題,首先如果這部分沒有設置,即使你后面關于axis的classpath設置的完全正確,在編寫客戶端測試Web?Service?是也會出現錯誤。而且這時候,你的axis已經可以運行,你寫的簡單的HelloWorld.jws也沒什么問題,能看到相應的SOAP消息。但是你的客戶端程序卻出問題了,你可以正常編譯,不會出現錯誤。但運行時會出現諸如Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/axis/client/Service此類的異常,這種錯誤通常是最令人痛苦的。
關于Tomcat的classpath一要注意,如果你是通過Windows安裝程序安裝時,這是你的系統中已經有了TOMCAT_HOME,和JAVA_HOME 環境變量,此時你要做的就是添加classpath環境變量(如果你還沒有添加的話,具體做法我就不用說了,很簡單的基礎知識),classpath的設置如下
classpath=.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar(
.;是必需的,它指代當前目錄)
然后修改環境變量中的classpath,把tomat安裝目錄下的common\lib下的(可以根據實際追加)servlet.jar追加到classpath中去,修改后的classpath如下:
classpath=.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;%CATALINA_HOME%\common\lib\servlet.jar;
4.接下來就是關于axis的環境變量的設置了,向導上講的很清楚。就把原文直接考過來
On Windows, this can be done via the following. For this document we assume that you have installed Axis in C:\axis. To store this information permanently in WinNT/2000/XP you will need to right click on "My Computer" and select "Properties". Click the "Advanced" tab and create the new environmental variables. It is often better to use WordPad to create the variable string and then paste it into the appropriate text field.
set AXIS_HOME=c:\axis
set AXIS_LIB=%AXIS_HOME%\lib
set AXISCLASSPATH=%AXIS_LIB%\axis.jar;%AXIS_LIB%\commons-discovery.jar;
%AXIS_LIB%\commons-logging.jar;%AXIS_LIB%\jaxrpc.jar;%AXIS_LIB%\saaj.jar;
%AXIS_LIB%\log4j-1.2.8.jar;%AXIS_LIB%\xml-apis.jar;%AXIS_LIB%\xercesImpl.jar
注意一點,.jar文件要與你的解壓的axis目錄中的文件名一致,由于版本問題,文件名可能不一致。
5 . 測試
下面是一個簡單的WebService, 供測試使用
(代碼是轉貼,呵呵)
import?java.util.*;
?
public?class?wsTestService


{
??String?strName?=?"Ryun";
??int?intAge?=?21;
??List?items?=?new?ArrayList();
??

??public?String?getName()?
{
????return?strName;
???}
??

??public?int?getAge()?
{
????return?intAge;
???}
??

??public?List?getItems()?
{
????return?items;
???}
??}

將此文件命名為 wsTestService.jws 存放到
Tomcat的\webapp\axis\目錄下
訪問
http://localhost:8080/axis/wsTestService.jws, 出現 "There is a Web
Service here"即表明 Web Service 服務端程序安裝完成.
開發一個 Web Service 客戶端程序
建立客戶端程序wsTestClient.java
以下是客戶端的源程序, 用于向 Web Service Server 提交服務請求:
import?org.apache.axis.client.Call;
import?org.apache.axis.client.Service;
?

public?class?wsTestClient?
{

??public?static?void?main(String?args[])?
{
????System.out.println("Start?invoking
");

????try?
{
??????String?strUri?=?

"http://localhost:8080/axis/wsTestService.jws";
??????Service?service?=?new?Service();
??????Call?call?=?(Call)
??????service.createCall();
??????call.setTargetEndpointAddress(new?

java.net.URL(strUri));
??????call.setOperationName("getName");

??????String?ret?=?""?+?call.invoke(new?Object[]?
{});
??????System.out.println("I?am?"?+?ret?+?".");
?????}

????catch?(Exception?e)?
{
??????System.err.println(e.toString());
?????}
????System.out.println("Finished?the?invoking.");
????}
}然后編譯運行,如果出現問題,那可能是環境變量path,classpath配置的問題,自己查看
一下,這里就不再描述了。
C:\>cd \
C:\>javac wsTestClient.java
?
C:\>java wsTestClient
Start invoking...
I am Ryun.
Finished the invoking.
完成,再去學習,稍后總結
但這時還有一個問題,我還沒有解決,關于log4j的一個warn,不知道該如何配置log4j,懶得再去找答案了。axis的相關問題搜索了兩天了才有個結果,log4j的問題以后有時間再弄吧。