Axis中操作Header的資訊是通過Handler來完成的,在讀取Header的資訊的時候,可以完成Header的操作的Handler有JWSHandler、SOAPMonitorHandler、JAXRPCHandler。JWSHandler是完成jws釋出模式的WebService的Header的讀取操作,
SOAPMonitorHandler是完成SOAPMonitor模式的,JAXRPCHandler是java rpc模式的。
下面将會用JAXRPCHandler讀取用戶端的Header資訊,然後用BasicHandler将用戶端請求的Header寫入用戶端的響應的Header中。
1、 首先編寫一個方法,這個方法什麼也不做。就是在這個方法請求和響應的時候分别完成讀取Header的資訊和寫入響應的Header内容。代碼如下:
代碼
2、 用戶端的Header中的内容是下面這樣的xml内容
下面将用用戶端代碼将上面的xml元素添加的Header中,代碼如下:
上面的就完成了xml中的内容,并且添加到請求的Header中。
3、 根據上面的Header的内容,我們現在編寫一個Handler用來讀取用戶端請求資訊中的Header的内容,這個Handler需要繼承JAXRPCHandler。代碼如下:
上面讀取Header内容的代碼有點小長,不過比較簡單。就是用循環周遊Header的内容,Header的内容是一個xml的文檔。是以用循環一步步向裡面周遊,不過感覺寫個遞歸會更簡單。
4、 下面将編寫一個Handler來向用戶端的Response中寫入Header内容,寫入的Header的内容就是用戶端請求的Header内容。代碼如下:
上面的代碼是取出respHeader = (SOAPHeader) respSe.getHeader();
然後循環周遊Header的元素寫入到respHeader.addChildElement(she);
5、 好了,上面已經寫好了WebService的方法,以及請求時讀取Header内容的Handler和在響應時寫入Header内容的Handler。下面開始寫wsdd釋出目前WebService。
還是用doc指令釋出目前WebService,指令如下:
C:\SoftWare\tomcat-5.0.28\tomcat-5.0.28\webapps\AxisWebService\WEB-INF>java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -lhttp://localhost:8080/AxisWebService/services/AdminService deployHeader.wsdd
釋出完成後,你會看的
Processing file deployHeader.wsdd
<Admin>Done processing</Admin>
的字樣就表示釋出成功了,然後在浏覽器位址欄輸入:
<a href="http://localhost:8080/AxisWebService/servlet/AxisServlet">http://localhost:8080/AxisWebService/servlet/AxisServlet</a>
你就可以看到OperaterHandler這個WebService了
6、 OperaterHandler這個WebService成功釋出,現在就是要編寫用戶端代碼調用這個WebService,用戶端代碼比較簡單,完整代碼如下:
call.getResponseMessage();可以得到伺服器響應的Header的資訊
運作後你可以看到用戶端控制台會輸出:
<ns1:Header soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" soapenv:mustUnderstand="0" xmlns:ns1="soap" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><ServiceName xsi:type="xsd:string" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">ReadHeaderService</ServiceName><CreateDate xsi:type="xsd:dateTime" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">2010-12-19T06:22:59.931Z</CreateDate><ns2:Version xsi:type="xsd:string" xmlns:ns2="service" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">1.0</ns2:Version><author><name xsi:type="xsd:string" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">axis</name><age xsi:type="xsd:string" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">14</age></author><copyRight><year xsi:type="xsd:string" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">2010</year><number xsi:type="xsd:string" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">123123123</number></copyRight><Tel xsi:type="xsd:string" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">555666442</Tel></ns1:Header>
responseMessage:<soapenv:Header xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><ns1:Header soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" soapenv:mustUnderstand="0" xmlns:ns1="soap"><ServiceName xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">ReadHeaderService</ServiceName><CreateDate xsi:type="xsd:dateTime" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">2010-12-19T06:22:59.931Z</CreateDate><ns2:Version xsi:type="soapenc:string" xmlns:ns2="service" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">1.0</ns2:Version><author><name xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">axis</name><age xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">14</age></author><copyRight><year xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">2010</year><number xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">123123123</number></copyRight><Tel xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">555666442</Tel></ns1:Header></soapenv:Header>
responseMessage前面的内容是用戶端将會寫入到Header中的xml内容,而responseMessage後面的内容就是響應的Header的内容,這段内容是WriteHeaderHandler寫入到ResponseMessage中的。
再看看伺服器端控制台的内容:
父節點:Header, 節點:ServiceName, 值:ReadHeaderService
父節點:Header, 節點:CreateDate, 值:2010-12-19T06:22:59.931Z
父節點:Header, 節點:Version, 值:1.0
父父節點:Header, 父節點:author, 節點:name##name, 值:axis##axis
父父節點:Header, 父節點:author, 節點:age##age, 值:14##14
父父節點:Header, 父節點:copyRight, 節點:year##year, 值:2010##2010
父父節點:Header, 父節點:copyRight, 節點:number##number, 值:123123123##123123123
父節點:Header, 節點:Tel, 值:555666442
ns1:Header
header Info:<soapenv:Header xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><ns1:Header soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" soapenv:mustUnderstand="0" xmlns:ns1="soap"><ServiceName xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">ReadHeaderService</ServiceName><CreateDate xsi:type="xsd:dateTime" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">2010-12-19T06:22:59.931Z</CreateDate><ns2:Version xsi:type="soapenc:string" xmlns:ns2="service" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">1.0</ns2:Version><author><name xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">axis</name><age xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">14</age></author><copyRight><year xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">2010</year><number xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">123123123</number></copyRight><Tel xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">555666442</Tel></ns1:Header></soapenv:Header>
上面的文本内容是ReadHeaderHandler讀取用戶端的Header的内容和我們定制的xml的内容是一緻的。而後面的xml能入是WriteHeaderHandler中輸出的内容。
本文轉自hoojo部落格園部落格,原文連結:http://www.cnblogs.com/hoojo/archive/2010/12/20/1911396.html,如需轉載請自行聯系原作者