监听器(统计在线人数)

监听器(统计在线人数)

监听器是GUI中常用的功能,Javaweb中使用场景不是很多,可以使用监听器实现统计网站访问人数;

实现原理

服务器回味每个客户端创建一个session,通过监听服务器中session的创建和销毁便可以记录当前访问网站的人数;

实现步骤

  • 实现HttpSessionListener类

    package com.kangzhu.listener;
    
    import javax.servlet.ServletContext;
    import javax.servlet.http.HttpSessionEvent;
    import javax.servlet.http.HttpSessionListener;
    
    public class OnlineCountListener implements HttpSessionListener {
    
        public void sessionCreated(HttpSessionEvent se) {
            ServletContext servletContext = se.getSession().getServletContext();
            Integer count = (Integer) servletContext.getAttribute("count");
            if (count == null) {
                count = 1;
            }else {
                count += 1;
            }
            servletContext.setAttribute("count", count);
        }
    
        public void sessionDestroyed(HttpSessionEvent se) {
            ServletContext servletContext = se.getSession().getServletContext();
            Integer count = (Integer) servletContext.getAttribute("count");
            if (count == null) {
                count = 0;
            }else {
                count -= 1;
            }
            servletContext.setAttribute("count", count);
        }
    }
    

    对Session的监听需要实现两个方法,Session的创建和销毁,需要注意的是需要将统计的在线人数存放在ServletContext对象中;

  • 在web.xml中进行注册

    <listener>
        <listener-class>com.kangzhu.listener.OnlineCountListener</listener-class>
    </listener>
    
  • 前端获取访问人数

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
      <head>
        <title>$Title$</title>
      </head>
      <body>
      <h1>当前网站的有<%=request.getSession().getServletContext().getAttribute("count")%>人访问</h1>
      </body>
    </html>
    
  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
由于我是AI语言模型,无法进行图形用户界面的编程和网络开发,但是我可以提供一些思路和代码示例。 生成登陆页面可以使用Java的Swing GUI库,可以通过创建一个JFrame窗口、放置各种Swing组件来实现。以下是一个简单的登陆页面示例: ``` import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class LoginFrame extends JFrame implements ActionListener { private JButton loginButton; private JTextField usernameField; private JPasswordField passwordField; public LoginFrame() { super("Login"); // 创建组件 JLabel usernameLabel = new JLabel("Username:"); JLabel passwordLabel = new JLabel("Password:"); usernameField = new JTextField(); passwordField = new JPasswordField(); loginButton = new JButton("Login"); // 设置布局 setLayout(new GridLayout(3, 2)); add(usernameLabel); add(usernameField); add(passwordLabel); add(passwordField); add(new JLabel()); add(loginButton); // 添加事件监听器 loginButton.addActionListener(this); // 设置窗口属性 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setLocationRelativeTo(null); setVisible(true); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == loginButton) { String username = usernameField.getText(); String password = new String(passwordField.getPassword()); // TODO: 处理登陆逻辑 } } } ``` 统计在线人数可以通过在服务器端记录用户的登陆状态来实现。具体实现方式可以根据具体的网络架构和技术栈选择不同的方案。以下是一个简单的示例: ``` import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; import java.util.List; public class OnlineCounterServer { public static void main(String[] args) { List<Socket> clients = new ArrayList<>(); try (ServerSocket serverSocket = new ServerSocket(8888)) { System.out.println("Server started."); while (true) { Socket client = serverSocket.accept(); clients.add(client); System.out.println("Client " + client.getRemoteSocketAddress() + " connected. Online count: " + clients.size()); } } catch (IOException e) { e.printStackTrace(); } } } ``` 以上示例中,服务器监听8888端口,每当有客户端连接时,就将客户端Socket加入到一个列表中,并输出在线人数。实际项目中可能需要更复杂的实现,例如使用并发编程技术来提高性能、使用数据库来记录用户状态等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值