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);
}
}
申明:次例子比较浅陋,只是个人学习笔录。