目录
前言必读
一、概念
Listener表示监听器。是Javaweb三大组件(Servlet、Filter、Listener)之一
优先级为监听器>过滤器>servlet
监听器可以监听就是在application,session,request三个对象创建、销毁或者往其中添加修改删除属性时自动执行代码的功能组件
1.Listener分类:JavaWeb提供了8个监听器
2.监听器的作用
统计在线人数和在线用户
统计网站访问量
系统启动时加载初始化信息
和spring结合
二、代码
package com.project.JavaScript;
import javax.servlet.*;
import javax.servlet.annotation.*;
@WebListener
public class Listener1 implements ServletContextListener{
public Listener1() {
}
@Override
public void contextInitialized(ServletContextEvent sce) {
/* This method is called when the servlet context is initialized(when the Web application is deployed). */
//初始化方法里面加载资源
System.out.println("contextLoaderListener...");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
/* This method is called when the servlet Context is undeployed or Application Server shuts down. */
//销毁方法
}
}
如果你觉得本篇文章对你有所帮助的,给个一键三连吧!!!
点赞关注收藏,抱拳了!