WebService大讲堂之Axis2(5):会话(Session)管理
发布: 2009-2-06 13:46 | 作者: yunzhmb | 来源: 青软培训-踏踏实实育人-勤勤恳恳奉献
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类,代码如下:
<!--
code highlighting produced by actipro codehighlighter (freeware)
http://www.codehighlighter.com/
-->package service;
import org.apache.axis2.context.servicecontext;
import org.apache.axis2.context.messagecontext;
public class loginservice
{
public boolean login(string username, string password)
{
if("bill".equals(username) && "1234".equals(password))
{
// 第1步:设置key-value对
messagecontext mc = messagecontext.getcurrentmessagecontext();
servicecontext sc = mc.getservicecontext();
sc.setproperty("login", "成功登录");
return true;
}
else
{
return false;
}
}
public string getloginmsg()
{
// 第1步:获得key-value对中的value
messagecontext mc = messagecontext.getcurrentmessagecontext();
servicecontext sc = mc.getservicecontext();
return (string)sc.getproperty("login");
}
}
<style type="text/css">
<!--
/* font definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:""@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;}
/* style definitions */
p.msonormal, li.msonormal, div.msonormal
{mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
font-size:10.5pt;
font-family:"times new roman";}
/* page definitions */
@page
{}
@page section1
{size:612.0pt 792.0pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;}
div.section1
{page:section1;}
--></style>
在loginservice类中有两个方法:login和getloginmsg,如果login方法登录成功,会将“成功登录”字符串保存在servicecontext对象中。如果在login方法返回true后调用getloginmsg方法,就会返回“成功登录”。
下面是loginservice类的配置代码(services.xml):
<!--
code highlighting produced by actipro codehighlighter (freeware)
http://www.codehighlighter.com/
--><!-- 第2步:添加scope属性 -->
<service name="loginservice" scope="transportsession">
<description>
登录服务
</description>
service.loginservice
</parameter>
<messagereceivers>
<messagereceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
class="org.apache.axis2.rpc.receivers.rpcmessagereceiver" />
</messagereceivers>
</service>
<style type="text/css">
<!--
/* font definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:""@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;}
/* style definitions */
p.msonormal, li.msonormal, div.msonormal
{mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
font-size:10.5pt;
font-family:"times new roman";}
/* page definitions */
@page
{}
@page section1
{size:612.0pt 792.0pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;}
div.section1
{page:section1;}
--></style>
使用如下的命令生成客户端使用的stub类:
<!--
code highlighting produced by actipro codehighlighter (freeware)
http://www.codehighlighter.com/
-->%axis2_home%\bin\wsdl2java -uri http://localhost:8080/axis2/services/loginservice?wsdl -p client -s -o stub
<style type="text/css">
<!--
/* font definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:""@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;}
/* style definitions */
p.msonormal, li.msonormal, div.msonormal
{mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
font-size:10.5pt;
font-family:"times new roman";}
/* page definitions */
@page
{}
@page section1
{size:612.0pt 792.0pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;}
div.section1
{page:section1;}
--></style>
在stub\src\client目录中生成了一个loginservicestub.java类,在该类中找到如下的构造句方法:
<!--
code highlighting produced by actipro codehighlighter (freeware)
http://www.codehighlighter.com/
-->public loginservicestub(org.apache.axis2.context.configurationcontext configurationcontext,
java.lang.string targetendpoint, boolean useseparatelistener)
throws org.apache.axis2.axisfault
{

_serviceclient.getoptions().setsoapversionuri(
org.apache.axiom.soap.soap12constants.soap_envelope_namespace_uri);
}
<style type="text/css">
<!--
/* font definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:""@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;}
/* style definitions */
p.msonormal, li.msonormal, div.msonormal
{mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
font-size:10.5pt;
font-family:"times new roman";}
/* page definitions */
@page
{}
@page section1
{size:612.0pt 792.0pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;}
div.section1
{page:section1;}
--></style>在该方法中最后添加如下的代码:
<!--
code highlighting produced by actipro codehighlighter (freeware)
http://www.codehighlighter.com/
-->// 第3步:打开客户端的session管理功能
_serviceclient.getoptions().setmanagesession(true);
<style type="text/css">
<!--
/* font definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:""@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;}
/* style definitions */
p.msonormal, li.msonormal, div.msonormal
{mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
font-size:10.5pt;
font-family:"times new roman";}
/* page definitions */
@page
{}
@page section1
{size:612.0pt 792.0pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;}
div.section1
{page:section1;}
--></style>
下面的客户端代码使用loginservicestub对象访问了刚才建立的webservice:
<!--
code highlighting produced by actipro codehighlighter (freeware)
http://www.codehighlighter.com/
-->loginservicestub stub = new loginservicestub();
loginservicestub.login login = new loginservicestub.login();
login.setusername("bill");
login.setpassword("1234");
if(stub.login(login).local_return)
{
system.out.println(stub.getloginmsg().local_return);
}
<style type="text/css">
<!--
/* font definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:""@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;}
/* style definitions */
p.msonormal, li.msonormal, div.msonormal
{mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
font-size:10.5pt;
font-family:"times new roman";}
/* page definitions */
@page
{}
@page section1
{size:612.0pt 792.0pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;}
div.section1
{page:section1;}
--></style>
运行上面的代码后,会输出“成功登录”信息。
读者可以想象,这对于一个完整的应用程序,无法保留状态,就意味着只依靠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类,代码如下:
<!--
code highlighting produced by actipro codehighlighter (freeware)
http://www.codehighlighter.com/
-->package service;
import org.apache.axis2.context.servicecontext;
import org.apache.axis2.context.messagecontext;
public class loginservice
{
public boolean login(string username, string password)
{
if("bill".equals(username) && "1234".equals(password))
{
// 第1步:设置key-value对
messagecontext mc = messagecontext.getcurrentmessagecontext();
servicecontext sc = mc.getservicecontext();
sc.setproperty("login", "成功登录");
return true;
}
else
{
return false;
}
}
public string getloginmsg()
{
// 第1步:获得key-value对中的value
messagecontext mc = messagecontext.getcurrentmessagecontext();
servicecontext sc = mc.getservicecontext();
return (string)sc.getproperty("login");
}
}
<style type="text/css">
<!--
/* font definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:""@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;}
/* style definitions */
p.msonormal, li.msonormal, div.msonormal
{mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
font-size:10.5pt;
font-family:"times new roman";}
/* page definitions */
@page
{}
@page section1
{size:612.0pt 792.0pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;}
div.section1
{page:section1;}
--></style>
在loginservice类中有两个方法:login和getloginmsg,如果login方法登录成功,会将“成功登录”字符串保存在servicecontext对象中。如果在login方法返回true后调用getloginmsg方法,就会返回“成功登录”。
下面是loginservice类的配置代码(services.xml):
<!--
code highlighting produced by actipro codehighlighter (freeware)
http://www.codehighlighter.com/
--><!-- 第2步:添加scope属性 -->
<service name="loginservice" scope="transportsession">
<description>
登录服务
</description>
service.loginservice
</parameter>
<messagereceivers>
<messagereceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
class="org.apache.axis2.rpc.receivers.rpcmessagereceiver" />
</messagereceivers>
</service>
<style type="text/css">
<!--
/* font definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:""@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;}
/* style definitions */
p.msonormal, li.msonormal, div.msonormal
{mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
font-size:10.5pt;
font-family:"times new roman";}
/* page definitions */
@page
{}
@page section1
{size:612.0pt 792.0pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;}
div.section1
{page:section1;}
--></style>
使用如下的命令生成客户端使用的stub类:
<!--
code highlighting produced by actipro codehighlighter (freeware)
http://www.codehighlighter.com/
-->%axis2_home%\bin\wsdl2java -uri http://localhost:8080/axis2/services/loginservice?wsdl -p client -s -o stub
<style type="text/css">
<!--
/* font definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:""@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;}
/* style definitions */
p.msonormal, li.msonormal, div.msonormal
{mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
font-size:10.5pt;
font-family:"times new roman";}
/* page definitions */
@page
{}
@page section1
{size:612.0pt 792.0pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;}
div.section1
{page:section1;}
--></style>
在stub\src\client目录中生成了一个loginservicestub.java类,在该类中找到如下的构造句方法:
<!--
code highlighting produced by actipro codehighlighter (freeware)
http://www.codehighlighter.com/
-->public loginservicestub(org.apache.axis2.context.configurationcontext configurationcontext,
java.lang.string targetendpoint, boolean useseparatelistener)
throws org.apache.axis2.axisfault
{

_serviceclient.getoptions().setsoapversionuri(
org.apache.axiom.soap.soap12constants.soap_envelope_namespace_uri);
}
<style type="text/css">
<!--
/* font definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:""@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;}
/* style definitions */
p.msonormal, li.msonormal, div.msonormal
{mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
font-size:10.5pt;
font-family:"times new roman";}
/* page definitions */
@page
{}
@page section1
{size:612.0pt 792.0pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;}
div.section1
{page:section1;}
--></style>在该方法中最后添加如下的代码:
<!--
code highlighting produced by actipro codehighlighter (freeware)
http://www.codehighlighter.com/
-->// 第3步:打开客户端的session管理功能
_serviceclient.getoptions().setmanagesession(true);
<style type="text/css">
<!--
/* font definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:""@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;}
/* style definitions */
p.msonormal, li.msonormal, div.msonormal
{mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
font-size:10.5pt;
font-family:"times new roman";}
/* page definitions */
@page
{}
@page section1
{size:612.0pt 792.0pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;}
div.section1
{page:section1;}
--></style>
下面的客户端代码使用loginservicestub对象访问了刚才建立的webservice:
<!--
code highlighting produced by actipro codehighlighter (freeware)
http://www.codehighlighter.com/
-->loginservicestub stub = new loginservicestub();
loginservicestub.login login = new loginservicestub.login();
login.setusername("bill");
login.setpassword("1234");
if(stub.login(login).local_return)
{
system.out.println(stub.getloginmsg().local_return);
}
<style type="text/css">
<!--
/* font definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:""@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;}
/* style definitions */
p.msonormal, li.msonormal, div.msonormal
{mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
font-size:10.5pt;
font-family:"times new roman";}
/* page definitions */
@page
{}
@page section1
{size:612.0pt 792.0pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;}
div.section1
{page:section1;}
--></style>
运行上面的代码后,会输出“成功登录”信息。




