黄色网页视频 I 影音先锋日日狠狠久久 I 秋霞午夜毛片 I 秋霞一二三区 I 国产成人片无码视频 I 国产 精品 自在自线 I av免费观看网站 I 日本精品久久久久中文字幕5 I 91看视频 I 看全色黄大色黄女片18 I 精品不卡一区 I 亚洲最新精品 I 欧美 激情 在线 I 人妻少妇精品久久 I 国产99视频精品免费专区 I 欧美影院 I 欧美精品在欧美一区二区少妇 I av大片网站 I 国产精品黄色片 I 888久久 I 狠狠干最新 I 看看黄色一级片 I 黄色精品久久 I 三级av在线 I 69色综合 I 国产日韩欧美91 I 亚洲精品偷拍 I 激情小说亚洲图片 I 久久国产视频精品 I 国产综合精品一区二区三区 I 色婷婷国产 I 最新成人av在线 I 国产私拍精品 I 日韩成人影音 I 日日夜夜天天综合

WebService大講堂之Axis2(5):會話(Session)

系統 2593 0

本文為原創,如需轉載,請注明作者和出處,謝謝!

WebService 給人最直觀的感覺就是由一個個方法組成,并在客戶端通過 SOAP 協議調用這些方法。這些方法可能有返回值,也可能沒有返回值。雖然這樣可以完成一些工具,但這些被調用的方法是孤立的,當一個方法被調用后,在其他的方法中無法獲得這個方法調用后的狀態,也就是說無法保留狀態。

讀者可以想象,這對于一個完整的應用程序,無法保留狀態,就意味著只依靠 WebService 很難完成全部的工作。例如,一個完整的應用系統都需要進行登錄,這在 Web 應用中使用 Session 來保存用戶登錄狀態,而如果用 WebService 的方法來進行登錄處理,無法保存登錄狀態是非常令人尷尬的。當然,這也可以通過其他的方法來解決,如在服務端使用 static 變量來保存用戶狀態,并發送一個 id 到客戶端,通過在服務端和客戶端傳遞這個 id 來取得相應的用戶狀態。這非常類似于 Web 應用中通過 Session Cookie 來管理用戶狀態。但這就需要由開發人員做很多工作,不過幸好 Axis2 為我們提供了 WebService 狀態管理的功能。

使用 Axis2 來管理 WebService 的狀態基本上對于開發人員是透明的。在 WebService 類需要使用 org.apache.axis2.context.MessageContext org.apache.axis2.context.ServiceContext 類來保存與獲得保存在服務端的狀態信息,這有些象使用 HttpSession 接口的 getAttribute setAttribute 方法獲得與設置 Session 域屬性。

除此之外,還需要修改 services.xml 文件的內容,為 <service> 元素加一個 scope 屬性,該屬性有四個可取的值: Application, SOAPSession, TransportSession, Request ,不過要注意一下,雖然 Axis2 的官方文檔將這四個值的單詞首字母和縮寫字母都寫成了大寫,但經筆者測試,必須全部小寫才有效,也就是這四個值應為: application soapsession transportsession request ,其中 request scope 屬性的默認值。讀者可以選擇使用 transportsession application 分別實現同一個 WebService 類和跨 WebService 類的會話管理。

在客戶端需要使用 setManageSession(true) 打開 Session 管理功能。

綜上所述,實現同一個 WebService Session 管理需要如下三步:

1. 使用 MessageContext ServiceContext 獲得與設置 key-value 對。

2. 為要進行 Session 管理的 WebService 類所對應的 <service> 元素添加一個 scope 屬性,并將該屬性值設為 transportsession

3. 在客戶端使用 setManageSession(true) 打開 Session 管理功能。

下面是一個在同一個 WebService 類中管理 Session 的例子。

先建立一個WebService類,代碼如下:


<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package service;
import org.apache.axis2.context.ServiceContext;
import org.apache.axis2.context.MessageContext;
public class LoginService
{
public boolean login(Stringusername,Stringpassword)
{
if ( " bill " .equals(username) && " 1234 " .equals(password))
{
// 第1步:設置key-value對
MessageContextmc = MessageContext.getCurrentMessageContext();
ServiceContextsc
= mc.getServiceContext();
sc.setProperty(
" login " , " 成功登錄 " );
return true ;
}
else
{
return false ;
}
}
public StringgetLoginMsg()
{
// 第1步:獲得key-value對中的value
MessageContextmc = MessageContext.getCurrentMessageContext();
ServiceContextsc
= mc.getServiceContext();
return (String)sc.getProperty( " login " );
}
}
<!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--> <!--[if gte mso 10]> <mce:style><! /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} --> <!--[endif]-->

LoginService 類中有兩個方法: login getLoginMsg ,如果 login 方法登錄成功,會將“成功登錄”字符串保存在 ServiceContext 對象中。如果在 login 方法返回 true 后調用 getLoginMsg 方法,就會返回“成功登錄”。

下面是 LoginService 類的配置代碼( services.xml ):

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> <!-- 第2步:添加scope屬性 -->
< service name ="loginService" scope ="transportsession" >
< description >
登錄服務
</ description >
< parameter name ="ServiceClass" >
service.LoginService
</ parameter >
< messageReceivers >
< messageReceiver mep ="http://www.w3.org/2004/08/wsdl/in-out"
class
="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</ messageReceivers >
</ service >

<!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--> <!--[if gte mso 10]> <mce:style><! /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} --> <!--[endif]-->

使用如下的命令生成客戶端使用的 stub 類:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> %AXIS2_HOME%\bin\wsdl2java-urihttp://localhost: 8080 /axis2/services/loginService?wsdl-pclient-s-ostub

<!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--> <!--[if gte mso 10]> <mce:style><! /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} --> <!--[endif]-->

stub\src\client 目錄中生成了一個 LoginServiceStub.java 類,在該類中找到如下的構造句方法:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> public LoginServiceStub(org.apache.axis2.context.ConfigurationContextconfigurationContext,
java.lang.StringtargetEndpoint,
boolean useSeparateListener)
throws org.apache.axis2.AxisFault
{

_serviceClient.getOptions().setSoapVersionURI(org.apache.axiom.soap.SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI);
}

<!--[if gte mso 10]> <mce:style><! /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} --> <!--[endif]--> 在該方法中最后添加如下的代碼:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> // 第3步:打開客戶端的Session管理功能
_serviceClient.getOptions().setManageSession( true );
<!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--> <!--[if gte mso 10]> <mce:style><! /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} --> <!--[endif]-->

下面的客戶端代碼使用 LoginServiceStub 對象訪問了剛才建立的 WebService

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> LoginServiceStubstub = new LoginServiceStub();
LoginServiceStub.Loginlogin
= new LoginServiceStub.Login();
login.setUsername(
" bill " );
login.setPassword(
" 1234 " );
if (stub.login(login).local_return)
{
System.out.println(stub.getLoginMsg().local_return);
}

<!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--> <!--[if gte mso 10]> <mce:style><! /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} --> <!--[endif]-->


運行上面的代碼后,會輸出“成功登錄”信息。

WebService大講堂之Axis2(5):會話(Session)管理


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論