如何检查所有用户登录到我的应用程序 [英] How can i check which all users are logged into my application
问题描述
我有一个基于网络的应用程序,它使用userName和密码进行登录。
I have a web based application that uses userName and password for login.
现在如何检查所有用户在该时间登录的特定时间。
i我正在使用会话管理,并且在应用程序中没有使用数据库,一切都在文件系统上
now how can i check on certain time which all users are logged in at that very time. i am using session management and no DB is used in application everything is on filesystem
编辑:1更傻的怀疑..如何定义应用范围的变量..这是这种东西吗?
1 more silly doubt.. how to define a variable with application scope.. is this something of this sort?
<env-entry>
<env-entry-name>test/MyEnv2</env-entry-name>
<env-entry-type>java.lang.Boolean</env-entry-type>
<env-entry-value>true</env-entry-value>
</env-entry>
推荐答案
只需收集<$ c中的所有登录用户$ c>在应用程序范围内设置。如果你的应用程序设计得很好,你应该有一个javabean User
代表登录用户。让它实现 HttpSessionBindingListener
并在 Set
中添加/删除用户在会话中即将绑定/未绑定时。
Just collect all logged in users in a Set
in the application scope. If your application is well designed, you should have a javabean User
which represents the logged-in user. Let it implement HttpSessionBindingListener
and add/remove the user from the Set
when it's about to be bound/unbound in the session.
Kickoff示例:
Kickoff example:
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!
}
请注意,您需要准备在应用程序范围中设置
,使其在上述方法中不返回 null
。您可以通过nullcheck在相同的方法中执行此操作,或者在 ServletContextListener#contextInitialized()
。
Note that you need to prepare the Set
in the application scope so that it doesn't return null
in above methods. You could do that in the same methods by a nullcheck, or with help of ServletContextListener#contextInitialized()
.
然后,在您的应用程序中您可以访问 ServletContext
的任何地方,就像在servlet中一样,您可以按如下方式访问已登录的用户:
Then, anywhere in your application where you've access to the ServletContext
, like in a servlet, you can just access the logged-in users as follows:
Set<User> logins = (Set<User>) getServletContext().getAttribute("logins");
这篇关于如何检查所有用户登录到我的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!