根据您的澄清:
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");