本文为原创,如需转载,请注明作者和出处,谢谢!
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 />
-->packageservice;
importorg.apache.axis2.context.ServiceContext;
importorg.apache.axis2.context.MessageContext;
publicclassLoginService
{
publicbooleanlogin(Stringusername,Stringpassword)
{
if("bill".equals(username)&&"1234".equals(password))
{
//第1步:设置key-value对
MessageContextmc=MessageContext.getCurrentMessageContext();
ServiceContextsc=mc.getServiceContext();
sc.setProperty("login","成功登录");
returntrue;
}
else
{
returnfalse;
}
}
publicStringgetLoginMsg()
{
//第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属性-->
<servicename="loginService"scope="transportsession">
<description>
登录服务
</description>
<parametername="ServiceClass">
service.LoginService
</parameter>
<messageReceivers>
<messageReceivermep="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 />
-->
publicLoginServiceStub(org.apache.axis2.context.ConfigurationContextconfigurationContext,
java.lang.StringtargetEndpoint,booleanuseSeparateListener)
throwsorg.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=newLoginServiceStub();
LoginServiceStub.Loginlogin=newLoginServiceStub.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 WebService大讲堂之Axis2 WebService大讲堂之Axis2 WebService大讲堂之Axis2 WebService大讲堂之Axis2 WebService大讲堂之Axis2
Axis2是目前比较流行的WebService引擎。...本课程站在初学者角度介绍了Axis2的大多数常用的功能,例如,以多种方式编写和发布WebService、JAX-RPC、JAX-WS、复杂类型传输、WebService会话管理、Axis2模块等
WebService大讲堂之Axis2,Axis2教程,axis2教程例子,Web_Service实例应用
webservice大讲堂之axis2.rar webservice大讲堂之axis2.rar webservice大讲堂之axis2.rar webservice大讲堂之axis2.rar webservice大讲堂之axis2.rar webservice大讲堂之axis2.rar
详细webservice大讲堂axis2
详细讲述了WebService 开发步骤
WebService大讲堂之Axis2(2):复合类型数据的传递.pdfWebService大讲堂之Axis2(2):复合类型数据的传递.pdf
webservice大讲堂之axis2,很入门的东西
WebService大讲堂之Axis2,多个实例,非常全的文档,从新手入门开始到复杂的实例
WebService大讲堂之Axis2(4):二进制文件传输.pdf
在Axis2中提供了一个Axis2模块(soapmonitor),该模块实现了与《WebService大讲堂之Axis2(9):编写Axis2模块(Module)》中实现的logging模块相同的功能,所不同的是,logging模块直接将SOAP请求与响应消息输出到...
WebService_大讲堂之Axis2_系列教程.pdf