Session处理同账号管理

首先使用HttpSessionAttributeListener监听session的属性添加
HttpSessionListener监听session的创建和销毁
session的invalidate方法 会删除属性同时触发attributeRemoved方法
监听器代码如下:
public class BindingListener implements HttpSessionListener,
HttpSessionAttributeListener {

private static List<LineUser> login_users = null;
private final int LIMIT_NUMS = 100;


static {
if (login_users == null) {
login_users = new ArrayList<LineUser>();
}

login_users = Collections.synchronizedList(login_users);
}

public void sessionCreated(HttpSessionEvent arg0) {
System.out.println(arg0.getSession().getId()
+ "session 创建时间:" + new Date());
}

public void sessionDestroyed(HttpSessionEvent arg0) {
System.out.println(arg0.getSession().getId()
+ "session 销毁时间:" + new Date());

}

public void attributeAdded(HttpSessionBindingEvent arg0) {
System.out.println("属性发生添加......");
String attr_name = arg0.getName();
String attr_value = (String)arg0.getValue();
int size = login_users.size();
LineUser user = null;
boolean flag = true;
String content = null;

if (Constant.USER_NAME.equals(attr_name)) {

for (int i = 0; i < size; i++) {
user = login_users.get(i);

if (user.getUserName().equals(attr_value)) {

// login_users.remove(user);
// user.getSession().invalidate();
// System.out.println(user.getSessionId() + " 失效......");

content = Constant.LOGIN_HAVE;
flag = false;

}
}

if (flag) {

if ((size + 1) < LIMIT_NUMS ) {

user = new LineUser();
user.setSession(arg0.getSession());
user.setSessionId(user.getSession().getId());
user.setUserName(attr_value);
login_users.add(user);
content = Constant.LOGIN_OK;
} else
content = Constant.LOGIN_LIMIT;
}

arg0.getSession().setAttribute(Constant.LOGIN_FLAG, content);

}

}

public void attributeRemoved(HttpSessionBindingEvent arg0) {
System.out.println("属性发生删除......");
String attr_name = arg0.getName();
String attr_value = (String)arg0.getValue();
int size = login_users.size();
LineUser user = null;

if (Constant.USER_NAME.equals(attr_name)) {

for (int i = 0; i < size; i++) {

user = login_users.get(i);
if (user.getUserName().equals(attr_value)) {

login_users.remove(user);
}

}
}

}

public void attributeReplaced(HttpSessionBindingEvent arg0) {
System.out.println("属性发生替换......");
String attr_name = arg0.getName();
String attr_value = (String)arg0.getValue();
int size = login_users.size();
LineUser user = null;

if (Constant.USER_NAME.equals(attr_name)) {

for (int i = 0; i < size; i++) {

user = login_users.get(i);

if (user.getUserName().equals(attr_value)
&& !user.getSessionId().equals(arg0.getSession().getId())) {

user.getSession().invalidate();
System.out.println(user.getSessionId() + " 已经失效......");

} else if (user.getSessionId().equals(arg0.getSession().getId())) {

user.setUserName(attr_value);
}

}
}

}

}

servlet实现如下:
public class LoginServlet extends HttpServlet {

protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doPost(req, resp);
}

protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

String name = req.getParameter("name");
String password = req.getParameter("password");

System.out.println("现在登录用户:" + name);

HttpSession session = req.getSession(true);
session.setAttribute(Constant.USER_NAME, name);

String content = (String)session.getAttribute(Constant.LOGIN_FLAG);
if (Constant.LOGIN_LIMIT.equals(content))
System.out.println("登录人数已经受限制");
else if (Constant.LOGIN_HAVE.equals(content))
System.out.println("账号已经登录");
else if (Constant.LOGIN_OK.equals(content))
System.out.println("登录成功");

req.getRequestDispatcher("/index.jsp").forward(req,resp);
}


}

申明:次例子比较浅陋,只是个人学习笔录。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值