监听器
Servlet规范中定义的一种特殊的组件,用来监听Servlet容器产生的事件并进行相应的处理
容器产生的两大类事件:
生命周期相关的事件
绑定数据相关的事件
绑定数据相关的事件
调用了request,session,ServletContext 的 setAttribute、removeAttribute方法时产生的事件
ServletRequestAttributeListener
attributeAdded(ServletRequestAttributeEvent srae)
attributeRemoved(ServletRequestAttributeEvent srae)
attributeReplaced(ServletRequestAttributeEvent srae)
HttpSessionAttributeListener
attributeAdded(HttpSessionBindingEvent event);
attributeRemoved(HttpSessionBindingEvent event);
attributeReplaced(HttpSessionBindingEvent event);
ServletContextAttributeListener
attributeAdded(ServletContextAttributeEvent event);
attributeRemoved(ServletContextAttributeEvent event);
attributeReplaced(ServletContextAttributeEvent event);
生命周期相关的事件
容器创建或者销毁request,session,ServletContext时产生的事件
ServletRequestListener
requestDestroyed(ServletRequestEvent sre)
requestInitialized (ServletRequestEvent sre)
HttpSessionListener
sessionCreated(HttpSessionEvent se)
sessionDestroyed(HttpSessionEvent se)
ServletContextListener
contextDestroyed(ServletContextEvent sce)
2、MyServletContextAttributeListener.java
3、截图
4、截图
Servlet规范中定义的一种特殊的组件,用来监听Servlet容器产生的事件并进行相应的处理
容器产生的两大类事件:
生命周期相关的事件
绑定数据相关的事件
绑定数据相关的事件
调用了request,session,ServletContext 的 setAttribute、removeAttribute方法时产生的事件
ServletRequestAttributeListener
attributeAdded(ServletRequestAttributeEvent srae)
attributeRemoved(ServletRequestAttributeEvent srae)
attributeReplaced(ServletRequestAttributeEvent srae)
HttpSessionAttributeListener
attributeAdded(HttpSessionBindingEvent event);
attributeRemoved(HttpSessionBindingEvent event);
attributeReplaced(HttpSessionBindingEvent event);
ServletContextAttributeListener
attributeAdded(ServletContextAttributeEvent event);
attributeRemoved(ServletContextAttributeEvent event);
attributeReplaced(ServletContextAttributeEvent event);
生命周期相关的事件
容器创建或者销毁request,session,ServletContext时产生的事件
ServletRequestListener
requestDestroyed(ServletRequestEvent sre)
requestInitialized (ServletRequestEvent sre)
HttpSessionListener
sessionCreated(HttpSessionEvent se)
sessionDestroyed(HttpSessionEvent se)
ServletContextListener
contextDestroyed(ServletContextEvent sce)
contextInitialized(ServletContextEvent sce)
绑定数据相关的事件案例:
1、web.xml
<!--绑定数据相关的事件-->
<listener>
<listener-class>com.listener.MyServletContextAttributeListener</listener-class>
</listener>
2、MyServletContextAttributeListener.java
package com.listener;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
public class MyServletContextAttributeListener implements ServletContextAttributeListener {
@Override
public void attributeAdded(ServletContextAttributeEvent servletContextAttributeEvent) {
System.out.println("attributeAdded...");
}
@Override
public void attributeRemoved(ServletContextAttributeEvent servletContextAttributeEvent) {
System.out.println("attributeRemoved...");
}
@Override
public void attributeReplaced(ServletContextAttributeEvent servletContextAttributeEvent) {
System.out.println("attributeReplaced...");
}
}
3、截图
生命周期相关的事件案例:
统计网站在线人数
1、web.xml
<!--监听器-->
<!--生命周期相关的事件-->
<listener>
<listener-class>com.listener.UserAccessCountListener</listener-class>
</listener>
2、index.jsp(使用el表达式)
在线人数: ${count}
3、UserAccessCountListener.java
package com.listener;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class UserAccessCountListener implements HttpSessionListener {
private void add(HttpSession session, int add) {
ServletContext context = session.getServletContext();
Object attribute = context.getAttribute("count");
int count = attribute == null ? 0 : (int) attribute;
synchronized (this) {
count += add;
}
context.setAttribute("count", count);
}
@Override
public void sessionCreated(HttpSessionEvent se) {
HttpSession session = se.getSession();
session.setMaxInactiveInterval(30);//单位为秒,设置为-1则永不过期
System.out.println(session.getId() + "被创建...");
add(session, 1);
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
HttpSession session = se.getSession();
System.out.println(session.getId() + "被销毁...");
add(session, -1);
}
}
4、截图