如何在Java中访问HTTP会话 [英] How to access HTTP sessions in Java

查看:74
本文介绍了如何在Java中访问HTTP会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过ID或Web应用程序(Java 2 EE)中所有当前活动的http会话以优雅的方式获取任何http会话?

How to get any http session by id or all currently active http sessions within web application (Java 2 EE) in an elegant way?

目前我有一个 WebSessionListener 一旦创建了会话,我把它放在 ConcurrentHashMap()(map.put(sessionId,sessionObj))中,一切好吧,我可以通过会话ID在任何时间从该地图检索HTTP会话,但它看起来像 HttpSession 对象永远不会完成...甚至会话无效地图仍然对无效会话对象的引用...我也阅读了这篇文章,看起来像 WeakHashMap 在我的情况下是不可接受的......

Currently I have a WebSessionListener and once session was created I put it in ConcurrentHashMap() (map.put(sessionId, sessionObj)), everything ok, I can retrieve HTTP session from that map in any time by session id, but it looks like the HttpSession objects will never finalize... Even session was invalidated the map still reference on invalidated session object... Also I have read this article and it looks like the WeakHashMap is not acceptable in my case...

In换句话说,我需要一个可能性来查看任何HttpSession甚至获取所有当前活动的 HttpSession 并从那里检索一些属性......

In other words I need a possiblity to look in any HttpSession even get all currently active HttpSession and retrieve some attributes from there...

请建议mebody:)

Please advice somebody :)

由于以下原因,我需要访问HttpSession对象:

I need to access HttpSession objects because of follwoing reason:

有时用户会做一些可能影响另一个并发用户工作的操作/请求,例如管理员应该禁用用户帐户但该用户当前正在使用该系统,在这种情况下我需要显示给管理员的消息,例如用户XXX目前正在使用该系统因此我需要检查是否有任何持有用户XXX凭据的HttpSession已经存在且处于活动状态。所以这就是我需要获得任何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.

我当前的实现是:SessionManager知道所有会话(ConcurrentMap)和HttpSessionListener将会话放入/删除SessionManager。

My current implementation is: SessionManager which knows about all sessions (ConcurrentMap) and HttpSessionListener which put/remove session into SessionManager.

我担心可能出现的内存问题,我想与某人讨论这个问题,但目前我清楚地看到一切都应该有效很好,因为当调用sessionDestroyed()方法时,所有无效的会话都将从地图中删除...

I was concerned about memory issues that may occure and I wanted to discusse this with someone, but currently I am clearly see that everything should works fine because all invalidated session will be removed from map when sessionDestroyed() method will be called...

非常感谢您的重播,但现在我明白这个问题只是想象力:)

Many thanks for your replays, but now I understood that problem was just imagination :)

推荐答案

根据您的澄清:


有时用户会做一些可能影响另一个并发用户工作的操作/请求,例如管理员应该禁用用户帐户但该用户当前正在使用该系统,在这种情况下我需要向我显示ssage to admin例如用户XXX目前正在使用该系统因此我需要检查是否有任何持有用户XXX凭据的HttpSession已经存在且处于活动状态。所以这就是我需要这样的可能性来获得任何http会话甚至是所有会话。

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

For this you actually don't need to know anything about the sessions. You just need to know which users are logged in. For that you can perfectly let the model object representing the logged in user implement HttpSessionBindingListener. I of course assume that you're following the normal idiom to login/logout user by setting/removing the User model as a session attribute.

public class User implements HttpSessionBindingListener {

    @Override
    public void valueBound(HttpSessionBindingEvent event) {
        Set<User> logins = (Set<User>) event.getSession().getServletContext().getAttribute("logins");
        logins.add(this);
    }

    @Override
    public void valueUnbound(HttpSessionBindingEvent event) {
        Set<User> logins = (Set<User>) event.getSession().getServletContext().getAttribute("logins");
        logins.remove(this);
    }

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

}

然后在您的管理应用中的某处,只需从中获取登录信息 ServletContext

Then somewhere in your admin app, just obtain the logins from ServletContext:

Set<User> logins = (Set<User>) servletContext.getAttribute("logins");

这篇关于如何在Java中访问HTTP会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆