展开
拓展 关闭
订阅号推广码
GitHub
视频
公告栏 关闭

javaweb开发(九):Listener监听器

  • 应用上下文全局监听器
# web.xml
    <context-param>
        <param-name>url</param-name>
        <param-value>https://xdclass.net</param-value>
    </context-param>

    <context-param>
        <param-name>topic</param-name>
        <param-value>java课堂</param-value>
    </context-param>

# 实体类
public class Config {

    private String url;

    private String topic;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getTopic() {
        return topic;
    }

    public void setTopic(String topic) {
        this.topic = topic;
    }

}

# 监听器
@WebListener
public class ContextListener implements ServletContextListener {

    /**
     * 初始化
     * @param sce
     */
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("ContextListener contextInitialized");
        // 从领域对象中获取数据
        ServletContext servletContext = sce.getServletContext();
        String url = servletContext.getInitParameter("url");
        String topic = servletContext.getInitParameter("topic");
        // 将数据设置到config对象中
        Config config = new Config();
        config.setTopic(topic);
        config.setUrl(url);
        // 将config对象设置到领域对象中
        servletContext.setAttribute("config",config);
    }

    /**
     * 销毁
     * @param sce
     */
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("ContextListener contextDestroyed");
    }

}

# 编写用于测试的servlet,获取领域对象中的数据
@WebServlet("/forward")
public class DispatchServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
        // 测试是否从web.xml 中获取到值
        Config config = (Config) req.getServletContext().getAttribute("config");
        System.out.println(config.getTopic());
        // 请求转发
        System.out.println("DispatchServlet doGet");
        req.getRequestDispatcher("/index.jsp").forward(req,response);
    }

}
  • 测试
http://localhost:8081/forward

# 控制台打印如下,说明监听器从web.xml中获取到了数据
java����
DispatchServlet doGet
  • SessionListener统计在线人数
# ContextListener中设置初始值
servletContext.setAttribute("onlineNum",0);

# 编写SessionListener 
@WebListener
public class SessionListener implements HttpSessionListener {

    /**
     * 初始化
     * @param se
     */
    @Override
    public void sessionCreated(HttpSessionEvent se) {
        System.out.println("SessionListener sessionCreated");
        ServletContext servletContext  = se.getSession().getServletContext();
        //获取在线人数
        Integer onlineNum = (Integer)servletContext.getAttribute("onlineNum");
        //新增1
        servletContext.setAttribute("onlineNum",++onlineNum);
    }

    /**
     * 销毁
     * @param se
     */
    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        System.out.println("SessionListener sessionDestroyed");
        ServletContext servletContext  = se.getSession().getServletContext();
        //获取在线人数
        Integer onlineNum = (Integer)servletContext.getAttribute("onlineNum");
        //减少1
        servletContext.setAttribute("onlineNum",--onlineNum);
    }

}

# 编写add.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>javaweb统计在线人数</title>
</head>
<body>

近30分钟在线人数: ${applicationScope.onlineNum}

</body>
</html>
  • 取消勾选

  • 启动测试

  • 使用RequestListener统计访问量

# ContextListener中设置初始访问值
servletContext.setAttribute("totalVisit",0);

# 编写RequestListener 
@WebListener
public class RequestListener implements ServletRequestListener {

    /**
     * 销毁的方法
     * @param sre
     */
    @Override
    public void requestDestroyed(ServletRequestEvent sre) {
        System.out.println(" RequestListener requestDestroyed");
    }

    /**
     * 初始化
     * @param sre
     */
    @Override
    public void requestInitialized(ServletRequestEvent sre) {
        System.out.println(" RequestListener requestInitialized");
        Integer totalVisit = (Integer)sre.getServletContext().getAttribute("totalVisit");
        System.out.println("历史总访问次数:"+totalVisit);
        totalVisit++;
        sre.getServletContext().setAttribute("totalVisit",totalVisit);
    }
    
}

# jsp中获取访问量
应用服务器启动后总访问次数:${totalVisit}
  • 测试
posted @ 2022-04-14 13:45  DogLeftover  阅读(14)  评论(0编辑  收藏  举报