登录的时候检查session应该就可以了啊,好像都是这么干的
解决方案 »
- 新手的servlet求救
- Struts<html text >标签问题
- 急求J2EE的API文档,csdn上的那个下载不下来!!!
- 有没有hibernate代码例子?
- java怎么样读取xml文档阿?希望大家能给点资料,最好是能给各例子 ,谢谢了
- tomcat的配置问题,高手进来看一下
- WebServices 到底是什么来的?
- 使用struts的时候,怎么才能在页面打开的时候,form里面已经填入我所需要的值?
- 活到老,学到老.javaEE我来了
- maven tomcat 插件 初始化时找不到jar包内的类和配置文件
- 关于 部署的问题:(在线等)
- 怎样把查询数据库得到的纪录用xml包的形式输出?高分!
登陆
if(users.Contains(username))
{
该用户己登陆!
}
else
{
user refUser=new user(username))
}
下面是一个在线人数的统计例子。
你可以很容易把在线人数改为在线用户。注册事件:
session.setAttribute("sessionObj",new sessionbean());
//sessionbean.java
package beans;
import java.util.Properties;
import javax.servlet.http.*;
public class sessionbean implements HttpSessionBindingListener
{
public sessionbean() {}
public void valueBound(HttpSessionBindingEvent e)
{Properties sys=System.getProperties();
String temp=(String)sys.get("online");
int online=1;
try {online=Integer.parseInt(temp);}
catch(Exception e1) {return;}
if(online<0) online=0;
sys.put("online",(online+1)+"");
System.setProperties(sys);
}
public void valueUnbound(HttpSessionBindingEvent e)
{Properties sys=System.getProperties();
String temp=(String)sys.get("online");
int online=0;
try {online=Integer.parseInt(temp);}
catch(Exception e1) {return;}
online--;
if(online<0) online=0;
sys.put("online",online+"");
System.setProperties(sys);
}
}
为什么不能,可以注销呀,
用户注销
user.Remove(String username);
希望说话负得起责任!!
我开始也想用application的attribute存放userList,关闭时remove掉。但是这种情况如何remove掉:用户登陆后离开了,过了一段时间在另一台计算上登录,而他先前的那个session已经过期了。如何让session过期触发userList的一个remove动作?to javahui(阶级斗争要年年讲,月月讲,天天讲。)
你的方法我先试试,好使的话,分少不了你的。^^to haode(好的), acefr():
是想用session中的user来比较。但我怎么从application中获得所有当前激活的session来一个个比较呀?
HttpSessionBindingListener 就是负责触发功能的。
也不是说你的方法是错误的。你的方法是对的。但是缺少SESSION失效检查的代码。user.Remove(String username);方法是可以,但是你什么时候调用这个方法?不可能要求用户退出时一定要点exit.jsp吧!
我查看了关于HttpSessionBindingListener相关的一些文档。的确,当实现了这个接口,在session过期或失效时,会发送消息给valueUnbound()方法。
javahui和xuzhenhua21也不用争论,二者结合起来就是一个完整的方案。
用javahui的例子,把方法valueBound()和valueUnbound()内容改写成xuzhenhua21的userList的方式(呵呵,Properties功能有限)。bound则在userList增加一个,unbound则在userList中删掉一个。应用程序中提供userList的是否包含新登录用户的验证方法。
是否就是这样子?
做为另外一种不需要建立userList的方案,我们能够直接获得当前所有有效的session,从而一个个比较呢?
准备结账!
import javax.servlet.http.*;
/**
*
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
用户登陆成功时注册事件:
session.setAttribute("sessionObj",new UserSession(username));
*/public class UserSession implements HttpSessionBindingListener
{ private String UserName;
public UserSession(String username)
{
this.UserName=username;
} public void valueBound(HttpSessionBindingEvent e)
{
Properties sys=System.getProperties(); int online;
try {online=((Integer)sys.get("online")).intValue();}
catch(Exception ex) {online=0;}
online++;
sys.put("online",new Integer(online)); ArrayList users=(ArrayList)sys.get("users");
users.add(this.UserName);
sys.put("users",users); System.setProperties(sys);
} public void valueUnbound(HttpSessionBindingEvent e)
{
Properties sys=System.getProperties(); int online;
try {online=((Integer)sys.get("online")).intValue();}
catch(Exception ex) {online=0;}
online--;
if(online<0) online=0;
sys.put("online",new Integer(online)); ArrayList users=(ArrayList)sys.get("users");
users.remove(this.UserName);
sys.put("users",users); System.setProperties(sys);
}}