java Session统计在线用户,并且显示在线用户

关键字: httpsession

1.http://www.jspcn.net/htmlnews/11049329478121583.html       监听器

 

2.session.invalidate() ,session才会destroy

 

3.HttpSessionListener:  这个监听取不到session里面的值

 

http://hi.baidu.com/tianshiyeben/blog/item/17d43923d695d042ad34de36.html

 

http://www.family168.com/tutorial/jsp/html/jsp-ch-04.html#jsp-ch-04-02     在线列表实例

下面的代码可以获取上线,下线的在线列表 :

 

public class OnlineListener implements HttpSessionListener ,HttpSessionAttributeListener{

 

            public void sessionCreated(HttpSessionEvent event) {//只要一打开浏览器就会执行 ,没有登陆也会执行.

                   }

            public void sessionDestroyed(HttpSessionEvent event) {//只有超时,invalidate()才会执行 
  
                   HttpSession se=event.getSession();
                  OnlineManager.getInstance().removeSession(se); //从列表中删除
               // System.out.println("remove session....................");//为什么浏览窗口关闭了,没有执行啊???
  
             }

         public void attributeAdded(HttpSessionBindingEvent event) {//如果登陆成功, 就把上线 用户添加 到列表.
              HttpSession se=event.getSession();
             String name=event.getName();
              String value=(String)event.getValue();
              if("username".equals(name)){
                      OnlineManager.getInstance().addSession(se); //添加 
             }
            }

}

 

public class OnlineManager {

 private static OnlineManager om;
 private  Map<String,HttpSession> sessions;
 private OnlineManager(){
  sessions=new HashMap<String,HttpSession>();//为什么没有共用一个sessions;
 }
 public static OnlineManager getInstance(){
  if(om==null){
   om=new OnlineManager();
  }
  return om;
 }
 public void addSession(HttpSession se){
  String key=(String)se.getAttribute("username");
  sessions.put(key, se);
  System.out.println("add 1 : "+sessions.size());
  
 }
 public void removeSession(HttpSession se){
  String key=(String)se.getAttribute("username");
  //sessions.remove(key);  //这个只是把key=null
  sessions.remove(sessions.get(key));
  System.out.println("remove 1 : " +"key:"+key+sessions.size());
  System.out.println(sessions);
 }
}
------------------------

第二种方法实现在线,下线:

public class BindSession implements HttpSessionBindingListener {
 
 private String username;
 
 public BindSession(String username){
  this.username=username;
 }
 public void valueBound(HttpSessionBindingEvent event) {
   HttpSession session = event.getSession();
  // String name=(String)session.getAttribute("name");
  
      ServletContext application = session.getServletContext();

      // 把用户名放入在线列表
      List onlineUserList = (List) application.getAttribute("onlineUserList");
      // 第一次使用前,需要初始化
      if (onlineUserList == null) {
          onlineUserList = new ArrayList();
          application.setAttribute("onlineUserList", onlineUserList);
      }
      onlineUserList.add(this.username);
      System.out.println("valueBound: .........."+onlineUserList.size());
 }

 public void valueUnbound(HttpSessionBindingEvent event) {
   HttpSession session = event.getSession();
  // String name=(String)session.getAttribute("name");
      ServletContext application = session.getServletContext();

      // 从在线列表中删除用户名
      List onlineUserList = (List) application.getAttribute("onlineUserList");
      onlineUserList.remove(this.username);

      System.out.println(this.username + "退出。");


 }

}

public class Login extends HttpServlet {

             protected void doPost(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {
  String name=req.getParameter("name");
  String pass=req.getParameter("password");
  boolean isLogin=false;
  int len=set.size();
  for(int i=0;i<len;i++){
   if(set.containsKey(name)&&set.containsValue(pass)){
    isLogin=true;
   }
  }
  
  if(isLogin){
   req.getSession().setAttribute("username", name);
   System.out.println("login ...username="+name);
   
   //BindListener 的使用:
   BindSession bl=new BindSession(name);
   req.getSession().setAttribute("lis", bl);
   resp.sendRedirect("index.jsp");
  }
  else{
   resp.sendRedirect("login.jsp");
  }

posted @ 2017-12-08 15:10  Rainyn  阅读(1006)  评论(0编辑  收藏  举报