監聽器是一個專門用於對其餘對象身上發生的事件或狀態改變進行監聽和相應處理的對象,當被監視的對象發生狀況時,當即採起相應的行動。監聽器其實就是一個實現特定接口的普通java程序,這個程序專門用於監聽另外一個java對象的方法調用或屬性改變,當被監聽對象發生上述事件後,監聽器某個方法當即被執行。java
JavaWeb中的監聽器是Servlet規範中定義的一種特殊類,它用於監聽web應用程序中的ServletContext, HttpSession和 ServletRequest等域對象的建立與銷燬事件,以及監聽這些域對象中的屬性發生修改的事件。web
共包括如下3類8種監聽器:spring
ServletContext建立與銷燬監聽:ServletContextListener數據庫
Httpsession的建立與銷燬監聽:HttpSessionListener緩存
HttpServletRequest建立與銷燬監聽:ServletRequestListenersession
包括屬性的添加,刪除,替換(key值同樣的時候,調用兩次set就會觸發這個事件)ide
ServletContex的屬性變化的監聽:ServletContextAttributeListenerurl
Httpsession的屬性變化的監聽:HttpSessionAttributeListenercode
HttpServletRequest的屬性的變化的監聽:ServletRequestAttributeListenerxml
保存在Session域中的對象能夠有多種狀態:
綁定session.setAttribute("bean",Object)到Session中;
從 Session域中解除session.removeAttribute("bean")綁定;
隨Session對象持久化到一個存儲設備中;
隨Session對象從一個存儲設備中恢復
Servlet 規範中定義了兩個特殊的監聽器接口"HttpSessionBindingListener和HttpSessionActivationListener"來幫助JavaBean 對象瞭解本身在Session域中的這些狀態,實現這兩個接口的類不須要註冊。
HttpSessionBindingListener:實現了HttpSessionBindingListener接口的JavaBean對象能夠感知本身被綁定到Session中和 Session中刪除的事件
HttpSessionActivationListener:實現了HttpSessionActivationListener接口的JavaBean對象能夠感知本身被活化(反序列化)和鈍化(序列化)的事件
@WebListener public class MyServletContextListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("-----------contextInitialized-------------"); } @Override public void contextDestroyed(ServletContextEvent sce) { System.out.println("-----------contextDestroyed-------------"); } }
能夠實如今系統啓動時獲取文件或數據庫中配置的全局參數、設置系統緩存、預加載內容等。
好比原來SSM項目web.xml文件中spring的配置:
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
ServletContextListener的第二個重要應用場景就是可使一些程序跟隨系統一塊兒運行,好比啓動定時任務。
如下是一個quartz定時的啓動demo:
public class DemoListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent servletContextEvent) { DemoManager demoManager = null; try { demoManager = new DemoManager(new StdSchedulerFactory().getScheduler()); } catch (SchedulerException e) { e.printStackTrace(); } demoManager.addJob(DemoJob.class,"demoJob","Demo","0/8 * * * * ? "); System.out.println("DemoListener 啓動!"); } @Override public void contextDestroyed(ServletContextEvent servletContextEvent) { } }
@WebServlet(name = "MyServlet2", urlPatterns = "/myServlet2") public class MyServlet2 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("----------doPost-----------"); doGet(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("----------doGet-----------"); ServletContext context = getServletContext(); context.setAttribute("name","123"); } }
@WebListener public class MyHttpSessionListener implements HttpSessionListener { @Override public void sessionCreated(HttpSessionEvent event) { System.out.println("session: " + event.getSession().getId() + " Created"); } @Override public void sessionDestroyed(HttpSessionEvent event) { System.out.println("session: " + event.getSession().getId() + " Destroyed"); } }
@WebListener public class MyHttpSessionListener implements HttpSessionListener { @Override public void sessionCreated(HttpSessionEvent event) { System.out.println("session: " + event.getSession().getId() + " Created"); ServletContext context = event.getSession().getServletContext(); Integer count = (Integer) context.getAttribute("onlineCount"); if (null == count) { count = 0; } else { count++; } context.setAttribute("onlineCount", count); } @Override public void sessionDestroyed(HttpSessionEvent event) { System.out.println("session: " + event.getSession().getId() + " Destroyed"); ServletContext context = event.getSession().getServletContext(); Integer count = (Integer) context.getAttribute("onlineCount"); if(null == count){ count = 0; }else{ count--; context.setAttribute("onlineCount", count); } } }