1. 什麼是監聽器html
2.監聽器最多見的應用場景:java
3. 監聽器術語web
4. 監聽器的編寫流程編程
5. JavaEE規範規定了8個監聽器接口用於監聽不一樣的web對象服務器
6. 域對象監聽器session
7. 域對象屬性監聽器(app
4大域對象:ServletContext 、Request、Session、PageContextide
7.1 做用域屬性操做編碼
setAttribute(k,v) getAttribute(k) removeAttribute(k)spa
7.2 域對象屬性變動監聽器
7.3 特殊的Javabean監聽器(Session做用域)
前面六個監聽器須要在web.xml中配置,最後的兩個監聽器不須要再web.xml中配置
8. 實例
8.1 ServletRequestListener
監聽request對象建立或銷燬,其中request對象的特色以下:
1 package com.eagle.listener; 2 3 import javax.servlet.ServletRequestEvent; 4 import javax.servlet.ServletRequestListener; 5 6 public class MyServletRequestListener implements ServletRequestListener { 7 8 public MyServletRequestListener() { 9 super(); 10 System.out.println("request對象的構造方法"); 11 } 12 13 public void requestInitialized(ServletRequestEvent sre) { 14 System.out.println("request建立了!"); 15 } 16 17 public void requestDestroyed(ServletRequestEvent sre) { 18 System.out.println("request對象銷燬了!"); 19 } 20 21 }
1 <listener> 2 <listener-class>com.eagle.listener.MyServletRequestListener</listener-class> 3 </listener>
服務器啓動就先構造了request對象,而後每一個頁面請求都會觸發
MyServletRequestListener.requestInitialized(ServletRequestEvent sre)方法和
MyServletRequestListener.requestDestroyed(ServletRequestEvent sre)方法
8.2 HttpSessionListener
1 package com.eagle.listener; 2 3 import javax.servlet.http.HttpSessionEvent; 4 import javax.servlet.http.HttpSessionListener; 5 6 public class MyHttpSessionListener implements HttpSessionListener { 7 8 public MyHttpSessionListener() { 9 10 super(); 11 System.out.println("session對象構造方法"); 12 } 13 14 public void sessionCreated(HttpSessionEvent arg0) { 15 System.out.println("建立session對象"); 16 } 17 18 public void sessionDestroyed(HttpSessionEvent arg0) { 19 System.out.println("銷燬session對象"); 20 } 21 22 }
1 package com.eagle.web; 2 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 import javax.servlet.http.HttpSession; 9 10 public class CreateSession extends HttpServlet { 11 12 private static final long serialVersionUID = 1L; 13 14 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 15 request.setCharacterEncoding("utf-8"); 16 response.setContentType("text/html;charset=utf-8"); 17 HttpSession httpSession = request.getSession(); 18 httpSession.setMaxInactiveInterval(10); 19 String id = httpSession.getId(); 20 if(httpSession.isNew()) { 21 httpSession.setAttribute("name", "狼"); 22 String name = (String) httpSession.getAttribute("name"); 23 response.getWriter().print("恭喜session建立成功,sessionid="+id+";name="+name); 24 }else { 25 String name = (String) httpSession.getAttribute("name"); 26 response.getWriter().print("服務器已經存在session了,sessionid="+id+";name="+name); 27 } 28 } 29 30 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 31 doGet(request, response); 32 } 33 }
1 <listener> 2 <listener-class>com.eagle.listener.MyHttpSessionListener</listener-class> 3 </listener>
8.3 servletContextListener
1 package com.eagle.listener; 2 3 import javax.servlet.ServletContextEvent; 4 import javax.servlet.ServletContextListener; 5 6 public class MyServletContextListener implements ServletContextListener { 7 8 public MyServletContextListener() { 9 System.out.println("ServletContext對象構建!"); 10 } 11 12 public void contextInitialized(ServletContextEvent sce) { 13 System.out.println("ServletContext對象初始化!"); 14 } 15 16 public void contextDestroyed(ServletContextEvent sce) { 17 System.out.println("ServletContext對象銷燬!"); 18 } 19 }
1 <listener> 2 <listener-class>com.eagle.listener.MyServletContextListener</listener-class> 3 </listener>
./shutdown.sh //中止Apache服務
8.4 ServletRequestAttributeListener
1 package com.eagle.web; 2 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 9 public class ServletRequestAttribute extends HttpServlet { 10 11 private static final long serialVersionUID = 1L; 12 13 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 14 // 編碼 15 request.setCharacterEncoding("utf-8"); 16 response.setContentType("text/html;charset=utf-8"); 17 request.setAttribute("name", "first"); 18 String name = (String) request.getAttribute("name"); 19 response.getWriter().print("name="+name); 20 request.removeAttribute("name"); 21 } 22 23 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 24 doGet(request, response); 25 } 26 }
1 package com.eagle.listener; 2 3 import javax.servlet.ServletRequestAttributeEvent; 4 import javax.servlet.ServletRequestAttributeListener; 5 6 public class MyRequestAttribute implements ServletRequestAttributeListener { 7 8 public MyRequestAttribute() { 9 super(); 10 System.out.println("requestattribute初始化!"); 11 } 12 13 public void attributeAdded(ServletRequestAttributeEvent sre) { 14 System.out.println("request對象添加了新屬性!"); 15 } 16 17 public void attributeRemoved(ServletRequestAttributeEvent sre) { 18 System.out.println("reqeust對象移除了屬性!"); 19 } 20 21 public void attributeReplaced(ServletRequestAttributeEvent sre) { 22 System.out.println("request修改了屬性!"); 23 } 24 25 }
<listener> <listener-class>com.eagle.listener.MyRequestAttribute</listener-class> </listener>
8.5 HttpSessionAttributeListener
1 package com.eagle.web; 2 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 import javax.servlet.http.HttpSession; 9 10 public class SessionAttribute extends HttpServlet { 11 12 private static final long serialVersionUID = 1L; 13 14 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 15 HttpSession session = request.getSession(); 16 session.setAttribute("name", "first"); 17 session.setAttribute("name", "second"); 18 session.getAttribute("name"); 19 session.removeAttribute("name"); 20 response.getWriter().write("hello haohao..."); 21 } 22 23 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 24 doGet(request, response); 25 } 26 }
1 package com.eagle.listener; 2 3 import javax.servlet.http.HttpSessionAttributeListener; 4 import javax.servlet.http.HttpSessionBindingEvent; 5 6 public class MySessionAttributeListener implements HttpSessionAttributeListener { 7 8 public MySessionAttributeListener() { 9 super(); 10 System.out.println("sessionAttributeListener初始化!"); 11 } 12 13 public void attributeAdded(HttpSessionBindingEvent hbe) { 14 System.out.println("sessionAttributeListener 添加了新屬性!"); 15 } 16 17 public void attributeRemoved(HttpSessionBindingEvent hbe) { 18 System.out.println("sessionAttributeListener 移除了屬性!"); 19 } 20 21 public void attributeReplaced(HttpSessionBindingEvent hbe) { 22 System.out.println("sessionAttributeListener 修改了屬性!"); 23 } 24 25 }
1 <listener> 2 <listener-class>com.eagle.listener.MySessionAttributeListener</listener-class> 3 </listener>
注:設置session引起request屬性修改監聽器
8.6 ServletContextAttributeListener
1 package com.eagle.web; 2 3 import java.io.IOException; 4 5 import javax.servlet.ServletContext; 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 public class ServletContextAttribute extends HttpServlet { 12 13 private static final long serialVersionUID = 1L; 14 15 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 16 ServletContext servletContext = request.getServletContext(); 17 servletContext.setAttribute("name", "first"); 18 servletContext.setAttribute("name", "second"); 19 servletContext.removeAttribute("name"); 20 response.getWriter().write("hello haohao..."); 21 } 22 23 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 24 doGet(request, response); 25 } 26 }
1 package com.eagle.listener; 2 3 import javax.servlet.ServletContextAttributeEvent; 4 import javax.servlet.ServletContextAttributeListener; 5 6 public class MyServletContextAttributeListener implements ServletContextAttributeListener { 7 8 public MyServletContextAttributeListener() { 9 super(); 10 System.out.println("ServletContextAttributeListener對象構造!"); 11 } 12 13 public void attributeAdded(ServletContextAttributeEvent sce) { 14 System.out.println("ServletContextAttributeListener對象添加屬性!"); 15 } 16 17 public void attributeReplaced(ServletContextAttributeEvent sce) { 18 System.out.println("ServletContextAttributeListener對象修改屬性!"); 19 } 20 21 public void attributeRemoved(ServletContextAttributeEvent sce) { 22 System.out.println("ServletContextAttributeListener對象移除屬性!"); 23 } 24 }
1 <listener> 2 <listener-class>com.eagle.listener.MyServletContextAttributeListener</listener-class> 3 </listener>
8.7 HttpSessionBindingListener
1 package com.eagle.web; 2 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 import javax.servlet.http.HttpSession; 9 10 import com.eagle.listener.MyHttpSessionBindingListener; 11 12 public class ServletHttpSessionBindingListener extends HttpServlet { 13 14 private static final long serialVersionUID = 1L; 15 16 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 17 MyHttpSessionBindingListener ml = new MyHttpSessionBindingListener(); 18 HttpSession hs = request.getSession(); 19 hs.setAttribute("ml", ml); 20 hs.invalidate(); 21 response.getWriter().write("hello haohao..."); 22 } 23 24 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 25 doGet(request, response); 26 } 27 }
1 package com.eagle.listener; 2 3 import javax.servlet.http.HttpSessionBindingEvent; 4 import javax.servlet.http.HttpSessionBindingListener; 5 6 public class MyHttpSessionBindingListener implements HttpSessionBindingListener { 7 8 public MyHttpSessionBindingListener() { 9 super(); 10 System.out.println("httpSessionBindingListening對象初始化"); 11 } 12 13 public void valueBound(HttpSessionBindingEvent hse) { 14 System.out.println("httpSessionBindingListening對象綁定!"); 15 } 16 17 public void valueUnbound(HttpSessionBindingEvent hse) { 18 System.out.println("httpSessionBindingListening對象解綁!"); 19 } 20 21 }
8.8 HttpSessionActivationListener
參照:https://www.cnblogs.com/eaglesour/p/9474540.html