免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 839 | 回复: 0
打印 上一主题 下一主题

跨多个WebService管理Session [复制链接]

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-21 08:44 |只看该作者 |倒序浏览
六、 跨多个WebService管理Session

当多个WebService的时候,我们要管理它的Session。这个时候我们得依靠ServiceGroupContext保存session信息;然后在发布WebService的时候,services.xml文件的的service表情的scope就不再说request或是transportsession了,而是application;最后同样要开启对session的管理,即options.setManageSession(true);

1、 首先多个WebService的session管理的代码如下:

package com.hoo.service; import org.apache.axis2.context.MessageContext; import org.apache.axis2.context.ServiceGroupContext; /** * <b>function:</b>管理多个会话Session信息 * @author hoojo * @createDate 2011-3-9 下午05:11:07 * @file LoginSessionService.java * @package com.hoo.service * @project Axis2WebService * @blog http://blog.csdn.net/IBM_hoojo * @email hoojo_@126.com * @version 1.0 */ public class LoginSessionService { public boolean login(String userName, String password) { MessageContext context = MessageContext.getCurrentMessageContext(); ServiceGroupContext ctx = context.getServiceGroupContext(); if ("admin".equals(userName) && "123456".equals(password)) { ctx.setProperty("userName", userName); ctx.setProperty("password", password); ctx.setProperty("msg", "登陆成功"); return true; } ctx.setProperty("msg", "登陆失败"); return false; } public String getLoginMessage() { MessageContext context = MessageContext.getCurrentMessageContext(); ServiceGroupContext ctx = context.getServiceGroupContext(); return ctx.getProperty("userName") + "#" + ctx.getProperty("msg"); } }

和上面的Session一样的操作,只不过是用ServiceGroupContext上下文来存取session信息

另外还需要用一个Service来查询session的信息,SearchService的代码如下:

代码

2、 编写services.xml来发布这2个服务,还以前不一样的。这一次是用一个services.xml文件配置2个service,同时发布2个服务。Xml代码如下:

代码

3、 发布完成后,可以通过http://localhost:8080/axis2/services/listServices查看发布的WebService服务,编写客户端的测试代码,code如下:

代码

运行后结果如下:

true

admin#登陆成功

找到的数据<userName, admin>

找到的数据<msg, 登陆成功>

找到的数据<password, 123456>

4、 如果将services.xml文件<service name="SearchSessionService" scope="application">的内容改成scope=transportsession,看看什么情况。是不是找不到session中的内容。

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP