JavaWeb -- sevlet 监听器
1.三个域对象的监听(创建和销毁) servletContext, session, request 监听器
ServletContext监听器:
public class MyServletContextListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("Context 初始化"); } @Override public void contextDestroyed(ServletContextEvent sce) { System.out.println("Context 销毁"); } }
web.xml 配置
<listener> <listener-class>com.kevin.web.listener.MyServletContextListener</listener-class> </listener>
Session监听器:
public class MyHttpSessionListener implements HttpSessionListener { @Override public void sessionCreated(HttpSessionEvent se) { System.out.println("Session 创建"); } @Override public void sessionDestroyed(HttpSessionEvent se) { System.out.println("Session 摧毁"); } }
Request监听器:
public class MyServletRequestListener implements ServletRequestListener { @Override public void requestInitialized(ServletRequestEvent sre) { System.out.println("request 创建"); } @Override public void requestDestroyed(ServletRequestEvent sre) { System.out.println("request 销毁"); } }
监听器还可以监听三个域中所添加的属性变化:
public class MyServletContextAttributeListener implements ServletContextAttributeListener { public void attributeAdded(ServletContextAttributeEvent scab) { System.out.println(scab.getValue()); System.out.println("加东西了!!"); } public void attributeRemoved(ServletContextAttributeEvent scab) { System.out.println("删东西了!!"); } public void attributeReplaced(ServletContextAttributeEvent scab) { System.out.println("替换东西了!!"); } }
public class MyRequestAndSessionAttributeListener implements HttpSessionAttributeListener, ServletRequestAttributeListener { public void attributeAdded(HttpSessionBindingEvent se) { System.out.println("session中加东西了!!"); } public void attributeRemoved(HttpSessionBindingEvent se) { System.out.println("session中删东西了!!"); } public void attributeReplaced(HttpSessionBindingEvent se) { System.out.println("session中替换东西了!!"); } public void attributeAdded(ServletRequestAttributeEvent srae) { System.out.println("request中加东西了!!"); } public void attributeRemoved(ServletRequestAttributeEvent srae) { System.out.println("request中删东西了!!"); } public void attributeReplaced(ServletRequestAttributeEvent srae) { System.out.println("request中替换东西了!!"); } }
HttpSessionBindingListener HttpSessionActivationListener 这两个监听器 不需要再web.xml文件中配置
public class MyBean implements HttpSessionBindingListener { private String name; public void valueBound(HttpSessionBindingEvent event) { System.out.println("自己被加到session中了"); } public void valueUnbound(HttpSessionBindingEvent event) { System.out.println("自己被session踢出来了"); } }
public class MyBean2 implements HttpSessionActivationListener,Serializable { public void sessionDidActivate(HttpSessionEvent se) { System.out.println("session从硬盘回到内存了"); } public void sessionWillPassivate(HttpSessionEvent se) { System.out.println("session被序列化到硬盘了"); } }
2. 实例: 在线人数计数器
public class OnLineCountListener implements HttpSessionListener { @Override public void sessionCreated(HttpSessionEvent se) { ServletContext context = se.getSession().getServletContext(); Integer num = (Integer) context.getAttribute("num"); if(num==null) context.setAttribute("num", 1); else { num++; context.setAttribute("num", num); } } @Override public void sessionDestroyed(HttpSessionEvent se) { ServletContext context = se.getSession().getServletContext(); Integer num = (Integer) context.getAttribute("num"); if(num==null) context.setAttribute("num", 1); else { num--; context.setAttribute("num", num); } } }
3. 实例: 自定义session扫描器
public class SessionScanerListener implements HttpSessionListener,ServletContextListener { private List<HttpSession> list = Collections.synchronizedList( new LinkedList<HttpSession>()); private Integer lock = 0; @Override public void contextInitialized(ServletContextEvent sce) { Timer timer = new Timer(); timer.schedule(new MyTask(list, lock) , 0, 1000*15); } @Override public void sessionCreated(HttpSessionEvent se) { HttpSession session = se.getSession(); synchronized (lock) { list.add(session); } System.out.println(session + "创建了"); } @Override public void sessionDestroyed(HttpSessionEvent se) { HttpSession session = se.getSession(); System.out.println(session + "销毁了"); } @Override public void contextDestroyed(ServletContextEvent sce) { // TODO Auto-generated method stub } } class MyTask extends TimerTask { private List<HttpSession> list; private Integer lock; public MyTask(List<HttpSession> list, Integer lock) { this.list = list; this.lock = lock; } @Override public void run() { synchronized (lock) { System.out.println("定时器启动了"); ListIterator<HttpSession> it = list.listIterator(); while(it.hasNext()) { HttpSession session = it.next(); if(System.currentTimeMillis()-session.getLastAccessedTime() > 1000*15) { session.invalidate(); list.remove(session); } } } } }