1、ServerContextListener
ServerContextListener 主要作用是用于监听应用启动/停止事件。有两个抽象方法contextInitialized()和contextDestroyed()。在web.xml配置ServerContextListener的实现类ActivityListener:
<listener>
<listener-class>com.wiki.listener.ActivityListener</listener-class>
</listener>
在应用启动时执行contextInitialized(), 在这个方法可以进行一些初始化操作,比如:启动一个时间任务自动执行。在调用该方法之后,容器才会在对Filter进行初始化。
当Servlet 容器终止Web 应用时调用contextDestroyed()方法来执行相关收尾操作。在调用该方法之前,容器会先销毁所有的Servlet 和Filter 过滤器。
参考网文:https://www.cnblogs.com/kingxiaozi/p/3980749.html
2、WebAppRootListener 这个listener的作用就是监听web.xml中的配置param-name为webAppRootKey的值:
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>myroot</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.util.WebAppRootListener
</listener-class>
</listener>
这个监听器会在web上下文初始化的时候,cs调用webUtil的对应方法,首先获取根传递进来的servletContext得到物理路径,String path=servletContext.getRealPath("/"); 然后找到context-param的webAooRootKey对应的param-value,把param-value的值作为key,上面配置的是"myroot", 接着执行System.setProperty("myroot",path)。这样在web中就可以使用System.getProperty("myroot")来获取系统的绝对路径。
然后配置一个然后配置一个监听器:
注:
1)如果只配置了监听器,没有配置webAppRootKey, 默认wenAppRootKey对应的param-value的值为webapp.root。
2)上面得到系统路径是Spring的做法,和平时自己采用的方法基本一致,都是写一个监听器,然后得到物理路径后手动放入System中,一般还会在这个监听器中加载配置文件,获取配置文件的值。
参考网文:https://blog.csdn.net/jintao_ma/article/details/52184011
3、WebAppRootListener 这个listener的作用是在启动Web容器时,自动装配Spring applicationContext.xml的配置信息。如果在web.xml中不写任何参数配置信息,默认的路径是"/WEB-INF/applicationContext.xml,在WEB-INF目录下创建的xml文件的名称必须是applicationContext.xml。如果是要自定义文件名可以在web.xml里加入contextConfigLocation这个context参数:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext-*.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
在<param-value> </param-value>里指定相应的xml文件名,如果有多个xml文件,可以写在一起并一“,”号分隔。上面的applicationContext-*.xml采用通配符,比如这那个目录下有applicationContext-ibatis-base.xml,applicationContext-action.xml,applicationContext-ibatis-dao.xml等文件,都会一同被载入。
如果applicationContext.xml文件没有在/WEB-INF/下,或文件名不一致,或存在多个Spring配置文件,在web.xml文件中根据下面代码修改
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext-*.xml,/WEB-INF/applicationContext.xml,/WEB-INF/classes/applicationContext-*.xml
</param-value>
</context-param>
参考网文:https://www.cnblogs.com/hello-yao-ge/p/5891435.html
4、HttpSessionListener 是用来监听session. 当用户第一次打开网页访问时将执行sessionCreated方法,在这个方法中可以进行一些操作,比如:统计在线用户,判断用户是已经登录。当用户关闭网页时将执行sessionDestroyed方法。可以进行用户退出等操作。