在java中访问http_如何在Java中访问HTTP会话

根据您的澄清:

Sometimes user does some actions/requests that may impact the work of another concurrent user, for example admin should disable user account but this user currently working with the system, in this case I need to show a message to admin e.g. “user XXX is currently working with the system” hence I need to check if any HttpSession which holds credentials of user XXX already exists and active. So this is whay I need such possibility to get any http session or even all sessions.

为此,您实际上不需要了解会话的任何信息.您只需要知道哪些用户已登录.为此,您可以完美地让代表登录用户的模型对象实现HttpSessionBindingListener.我当然假设您通过设置/删除来遵循登录/注销用户的正常习惯用法.用户模型作为会话属性.

public class User implements HttpSessionBindingListener {

@Override

public void valueBound(HttpSessionBindingEvent event) {

Set logins = (Set) event.getSession().getServletContext().getAttribute("logins");

logins.add(this);

}

@Override

public void valueUnbound(HttpSessionBindingEvent event) {

Set logins = (Set) event.getSession().getServletContext().getAttribute("logins");

logins.remove(this);

}

// @Override equals() and hashCode() as well!

}

然后在管理员应用程序的某个地方,只需从ServletContext获取登录信息:

Set logins = (Set) servletContext.getAttribute("logins");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值