监听器----如何使用监听器记录注册用户的在线状态?

Posted 波光闪烁

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了监听器----如何使用监听器记录注册用户的在线状态?相关的知识,希望对你有一定的参考价值。

首先写一个登陆界面----index.jsp

<body>
    <form action="${pageContext.request.contextPath }/loginServlet" method="post">
        用户名:<input name="uname"/><br/>
        密码:<input name="pwd" type="password"/><br/>
        <input type="submit" value="登录"/>
    </form>
  </body>

点击登陆,跳转至----loginServlet

@WebServlet(name="loginServlet",urlPatterns="/loginServlet") //Serclet3.0注解配置
public class LoginServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String name = request.getParameter("uname"); //获取用户名
        String pwd = request.getParameter("pwd");   //获取密码
        if("jack".equals(name) && "bdqn".equals(pwd)){  //判断用户名与密码是否正确
            request.getSession().setAttribute("name", name);   //将name放到session作用域里
            request.getRequestDispatcher("online.jsp").forward(request, response); //跳转至登录成功界面
        }else if ("tom".equals(name) && "123".equals(pwd)) { //第二个用户
            request.getSession().setAttribute("name", name);
            request.getRequestDispatcher("online.jsp").forward(request, response);
        }else {
            request.getRequestDispatcher("401.jsp").forward(request, response); //如果未成功登录,跳转至401界面,可返回重新登录
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}

未成功登陆----401.jsp

<body>
    请先登录,点击返回<a href="${pageContext.request.contextPath }/index.jsp">登录</a>页面
</body>

成功登录---online.jsp

<body>
    登录成功!当前在线人数:${applicationScope.num } <!-- 监听器获取在线人数 -->
    <a href="${pageContext.request.contextPath }/logoutServlet">退出</a>
</body>

退出处理----LogoutServlet

@WebServlet(name="logoutServlet",urlPatterns="/logoutServlet") 
public class LogoutServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.getSession().removeAttribute("name");
        request.getRequestDispatcher("index.jsp").forward(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}

 

设置监听器----首先需要在web.xml里配置

<listener>
      <listener-class>cn.bdqn.listener.DemoListemer</listener-class>
  </listener>

监听器类  DemoListemer  继承  HttpSessionAttributeListener  接口

public class DemoListemer implements HttpSessionAttributeListener{

    @Override
    public void attributeAdded(HttpSessionBindingEvent event) {
        int num=0;  //定义初始在线人数为0
        ServletContext servletContext = event.getSession().getServletContext();//使用session来判断是否为注册用户
        Object object = servletContext.getAttribute("num");  //获取人数num
        if(object==null){
            num=1;  //object为null,说明是第一个登录的用户
        }else {
            num=(Integer)object+1;  //如果不为null,就在原人数上+1;
        }
        servletContext.setAttribute("num", num);
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent event) {
        ServletContext servletContext = event.getSession().getServletContext();
        Object object = servletContext.getAttribute("num"); //获取人数num
        int num=(Integer)object-1;  //退出登录后人数-1
        servletContext.setAttribute("num", num);  //变更人数后设置给num
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent event) {
        // TODO Auto-generated method stub
        
    }
}

登录成功页面图

 

以上是关于监听器----如何使用监听器记录注册用户的在线状态?的主要内容,如果未能解决你的问题,请参考以下文章

仅仅通过Application监听用户行为及App的在线状态和在线时长

如何在消息侦听器中更新 ListView?

SwiftUI 利用 Combine 实时监听网络服务器在线状态的极简实现

SwiftUI 利用 Combine 实时监听网络服务器在线状态的极简实现

JavaWeb开发网站,监测用户在线状态

Java中的监听器