Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6534911
  • 博文数量: 915
  • 博客积分: 17977
  • 博客等级: 上将
  • 技术积分: 8846
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-26 09:59
个人简介

一个好老好老的老程序员了。

文章分类

全部博文(915)

文章存档

2022年(9)

2021年(13)

2020年(10)

2019年(40)

2018年(88)

2017年(130)

2015年(5)

2014年(12)

2013年(41)

2012年(36)

2011年(272)

2010年(1)

2009年(53)

2008年(65)

2007年(47)

2006年(81)

2005年(12)

分类: 系统运维

2011-05-12 23:42:36

六、 跨多个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; /** * function:管理多个会话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、 发布完成后,可以通过查看发布的WebService服务,编写客户端的测试代码,code如下:

代码

运行后结果如下:

true

admin#登陆成功

找到的数据

找到的数据

找到的数据

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

阅读(724) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~