ServletContextListener:监听web的启动和关闭。
package com.li.test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; /** * Application Lifecycle Listener implementation class ListerTest_1 * */ @WebListener public class ListerTest_1 implements ServletContextListener { /** * Default constructor. */ public ListerTest_1() {} /** * @see ServletContextListener#contextInitialized(ServletContextEvent) */ public void contextInitialized(ServletContextEvent event) { ServletContext context = event.getServletContext(); String driver = context.getInitParameter("driver"); String username = context.getInitParameter("username"); String password = context.getInitParameter("password"); String url = context.getInitParameter("url"); try { Class.forName(driver); Connection conn = DriverManager.getConnection(url, username, password); context.setAttribute("conn", conn); } catch (Exception e) { System.out.println("connect error: " + e.getMessage()); e.printStackTrace(); } } /** * @see ServletContextListener#contextDestroyed(ServletContextEvent) */ public void contextDestroyed(ServletContextEvent event) { ServletContext context = event.getServletContext(); Connection conn = (Connection) context.getAttribute("conn"); if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
ServletContextAttributeListener:监听application范围内的属性的变化。
package com.li.test; import javax.servlet.ServletContext; import javax.servlet.ServletContextAttributeEvent; import javax.servlet.ServletContextAttributeListener; import javax.servlet.annotation.WebListener; /** * Application Lifecycle Listener implementation class ListerTest_2 * */ @WebListener public class ListerTest_2 implements ServletContextAttributeListener { /** * Default constructor. */ public ListerTest_2() {} /** * @see ServletContextAttributeListener#attributeAdded(ServletContextAttributeEvent) */ public void attributeAdded(ServletContextAttributeEvent event) { ServletContext application = event.getServletContext(); String name = event.getName(); Object value = event.getValue(); System.out.println(application+"范围内添加了属性:"+name+",值为:"+value); } /** * @see ServletContextAttributeListener#attributeReplaced(ServletContextAttributeEvent) */ public void attributeReplaced(ServletContextAttributeEvent event) { ServletContext application = event.getServletContext(); String name = event.getName(); Object value = event.getValue(); System.out.println(application+"范围内删除了属性:"+name+",值为:"+value); } /** * @see ServletContextAttributeListener#attributeRemoved(ServletContextAttributeEvent) */ public void attributeRemoved(ServletContextAttributeEvent event) { ServletContext application = event.getServletContext(); String name = event.getName(); Object value = event.getValue(); System.out.println(application+"范围内修改了属性:"+name+",值为:"+value); } }
HttpSessionListener:监听session的开始和结束。
HttpSessionAttributeListener
HttpSessionActivationListener
HttpSessionBindingListener
package com.li.test; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.annotation.WebListener; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionAttributeListener; import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; /** * Application Lifecycle Listener implementation class ListerTest_4 * */ @WebListener public class ListerTest_4 implements HttpSessionListener, HttpSessionAttributeListener { /** * Default constructor. */ public ListerTest_4() {} /** * @see HttpSessionAttributeListener#attributeRemoved(HttpSessionBindingEvent) */ public void attributeRemoved(HttpSessionBindingEvent arg0) {} /** * @see HttpSessionAttributeListener#attributeAdded(HttpSessionBindingEvent) */ public void attributeAdded(HttpSessionBindingEvent arg0) {} /** * @see HttpSessionAttributeListener#attributeReplaced(HttpSessionBindingEvent) */ public void attributeReplaced(HttpSessionBindingEvent arg0) {} /** * @see HttpSessionListener#sessionCreated(HttpSessionEvent) */ public void sessionCreated(HttpSessionEvent sv) { HttpSession session = sv.getSession(); ServletContext application = session.getServletContext(); String sessionId = session.getId(); if (session.isNew()) { String user = (String) session.getAttribute("user"); user = (user == null) ? "游客" : user; Map<String, String> online = (Map<String, String>) application.getAttribute("online"); if (online == null) { online = new HashMap<String, String>(); online.put(sessionId, user); } application.setAttribute("online", online); } } /** * @see HttpSessionListener#sessionDestroyed(HttpSessionEvent) */ public void sessionDestroyed(HttpSessionEvent sv) { HttpSession session = sv.getSession(); ServletContext application = session.getServletContext(); String sessionId = session.getId(); Map<String, String> online = (Map<String, String>) application.getAttribute("online"); if (online != null) { online.remove(sessionId); } application.setAttribute("online", online); } }
ServletRequestListener
package com.li.test; import javax.servlet.ServletRequestEvent; import javax.servlet.ServletRequestListener; import javax.servlet.annotation.WebListener; import javax.servlet.http.HttpServletRequest; /** * Application Lifecycle Listener implementation class ListerTest_3 * */ @WebListener public class ListerTest_3 implements ServletRequestListener { /** * Default constructor. */ public ListerTest_3() {} /** * @see ServletRequestListener#requestDestroyed(ServletRequestEvent) */ public void requestDestroyed(ServletRequestEvent event) { HttpServletRequest request = (HttpServletRequest) event.getServletRequest(); System.out.println(request); } /** * @see ServletRequestListener#requestInitialized(ServletRequestEvent) */ public void requestInitialized(ServletRequestEvent event) { HttpServletRequest request = (HttpServletRequest) event.getServletRequest(); System.out.println(request); } }
ServletRequestAttributeListener