HttpServletRequest、HttpSession、ServletContext的生命周期都有对应的监听器监听
ServletContextListener
public interface ServletContextListener extends EventListener {
void contextInitialized(ServletContextEvent var1);
void contextDestroyed(ServletContextEvent var1);
}
在web容器启动和关闭时,会调用contextInitialized()和contextDestroyed()方法
/**
* 自定义ServletContextListener,在应用程序启动加载过程中,可以做数据库连接、配置某些参数操作等
*/
@WebListener
public class DIYServletContextListener implements ServletContextListener{
public DIYServletContextListener() {
}
public void contextInitialized(ServletContextEvent sce) {
ServletContext context = sce.getServletContext();
// 将妹纸放入context中,在web容器关闭之前,都可以通过context获取妹纸对象
// 如果想主动移除设置的妹纸,则可以context.removeAttribute()
context.setAttribute("meizhi",new Meizhi("meimeida", 17));
// 获取Context的初始化参数,WebListener注解没有设置初始化参数的功能,只能在web.xml中设置
String contextInitVal = context.getInitParameter("contextInitKey");
// 修改tomcat容器对每次会话的设置的ID,默认为JSESSIONID
SessionCookieConfig scconfig = context.getSessionCookieConfig();
scconfig.setName("NOT-JSESSIONID");
}
public void contextDestroyed(ServletContextEvent sce) {
}
}
在web.xml中自定义ServletContext的初始化参数
contextInitKey
下期大乐透中奖号码:235643215
除了配置@WebListener注解,也可以在web部署描述符中配置,如下:
DIYServletContextListener
ServletContextAttributeListener
环境上下文属性监听器,监听上下文属性的新增、移除和替换
public interface ServletContextAttributeListener extends EventListener {
void attributeAdded(ServletContextAttributeEvent var1);
void attributeRemoved(ServletContextAttributeEvent var1);
void attributeReplaced(ServletContextAttributeEvent var1);
}
当在ServletContext中添加属性、移除属性或替换属性,对应着attributeAdded()、attributeRemoved()、attributeReplaced()方法
@WebListener
public class DIYServletContextAttributeListener implements ServletContextAttributeListener {
@Override
public void attributeAdded(ServletContextAttributeEvent servletContextAttributeEvent) {
// 获取新增的属性名称和属性值
servletContextAttributeEvent.getName();
servletContextAttributeEvent.getValue();
}
@Override
public void attributeRemoved(ServletContextAttributeEvent servletContextAttributeEvent) {
// servletContext移除属性时,做的一些操作
}
@Override
public void attributeReplaced(ServletContextAttributeEvent servletContextAttributeEvent) {
// servletContext替换属性时,做的一些操作
}
}
HttpSessionListener
HttpSession生命周期监听器,关注着HttpSession的创建和回收
public interface HttpSessionListener extends EventListener {
void sessionCreated(HttpSessionEvent var1);
void sessionDestroyed(HttpSessionEvent var1);
}
可以通过HttpSessionEvent获取当前HttpSession,可以统计在线人数等
@WebListener
public class DIYHttpSessionListener implements HttpSessionListener{
@Override
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
// 获取刚创建好的HttpSession,进行一些操作
httpSessionEvent.getSession();
}
@Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
}
}
HttpSessionAttributeListener
public interface HttpSessionAttributeListener extends EventListener {
void attributeAdded(HttpSessionBindingEvent var1);
void attributeRemoved(HttpSessionBindingEvent var1);
void attributeReplaced(HttpSessionBindingEvent var1);
}
监听着HttpSession的属性新增、移除、替换等操作,可以通过HttpSessionBindingEvent获取到新增、移除或替换的属性名和属性值
HttpSessionBindingListener
实现该监听器无需做@WebListener注解配置或web.xml配置,但在使用的时候需要该监听器的实例,而HttpSessionListener只需要注解配置、web.xml配置就可以监听整个应用的所有session
public interface HttpSessionBindingListener extends EventListener {
void valueBound(HttpSessionBindingEvent var1);
void valueUnbound(HttpSessionBindingEvent var1);
}
当session做setAttribute()时,则调用valueBound方法,需要传入自身监听器的实例;当session做removeAttribute()时,则调用valueUnbound()方法
// 调用httpSessionBindingListener的valueBound()
session.setAttribute("diy-session-binding-listener", new DIYHttpSessionBindingListener());
// 调用httpSessionBindingListener的valueUnbound()
session.removeAttribute("diy-session-binding-listener");
HttpSessionActivationListener
@WebListener
public class DIYHttpSessionActivationListener implements HttpSessionActivationListener, Serializable {
@Override
public void sessionWillPassivate(HttpSessionEvent httpSessionEvent) {
// 将session从内存持久化到磁盘时,会调用此方法
}
@Override
public void sessionDidActivate(HttpSessionEvent httpSessionEvent) {
// 将session从磁盘反序列化到内存时,会调用此方法
}
}
Servlet规范不要求一个web容器支持分布式应用,但是他一定要支持HttpSessionActivationListener接口,以使代码可以支持分布式环境。
ServletRequestListener
监听请求对象的创建和回收
public interface ServletRequestListener extends EventListener {
void requestDestroyed(ServletRequestEvent var1);
void requestInitialized(ServletRequestEvent var1);
}
请求的生命周期比较短,所以两个方法总是“同时”的执行
@WebListener
public class DIYServletRequestListener implements ServletRequestListener {
@Override
public void requestDestroyed(ServletRequestEvent servletRequestEvent) {
}
@Override
public void requestInitialized(ServletRequestEvent servletRequestEvent) {
HttpServletRequest request = (HttpServletRequest) servletRequestEvent.getServletRequest();
request.setAttribute("233",233);
}
}
ServletRequestAttributeListener
请求属性变化的监听器,没啥好说的
public interface ServletRequestAttributeListener extends EventListener {
void attributeAdded(ServletRequestAttributeEvent var1);
void attributeRemoved(ServletRequestAttributeEvent var1);
void attributeReplaced(ServletRequestAttributeEvent var1);
}