基础8——监听器
1.说明:监听器的核心就是,你写完一个监听器,那么你这个监听器就是应用于整个项目的,而不同于以前,可能就只用于一个按钮,当然,也是有例外的,比如说这两个监听器:
,其是针对对象设置监听器的,他们叫做对象感应监听器,具体怎么使用,看下文。
2.监听器的种类
3.使用监听器的步骤
1)如果是域对象或者的是域对象内的属性的变化的话:
a.步骤如下:
(1)实现对于的接口
(2)实现对应的方法
b.触发条件:
前者就是创建和死亡这个周期,而后者是内容的变化。
案例一:利用ServletContextListener接口
package com.jiantingqi; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; /** * Application Lifecycle Listener implementation class ServletContext * */ @WebListener public class ServletContext implements ServletContextListener { /** * @see ServletContextListener#contextDestroyed(ServletContextEvent) */ public void contextDestroyed(ServletContextEvent arg0) { System.out.print("监听器死了"); } /** * @see ServletContextListener#contextInitialized(ServletContextEvent) */ public void contextInitialized(ServletContextEvent arg0) { System.out.print("监听器被启动了"); } }
测试:直接运行,在console里面就可以看到“监听器被启动了”的信息,而关闭服务器,就可以看到“监听器死了”这条信息。
注意:其余两个域的生命周期的案例我就不写了,你就记住它的生命周期是什么,然后根据你的需求去应用就可以了。然后两个域的生命周期如下:
案例二:域对象内容变化监听器HttpSessionAttibuteListener
1)监听器内容
package com.jiantingqi; import javax.servlet.annotation.WebListener; import javax.servlet.http.HttpSessionAttributeListener; import javax.servlet.http.HttpSessionBindingEvent; /** * Application Lifecycle Listener implementation class HttpSessionAttibuteListener * */ @WebListener public class HttpSessionAttibuteListener implements HttpSessionAttributeListener { public void attributeAdded(HttpSessionBindingEvent arg0) { System.out.println("Session里面加数据了"); } /** * @see HttpSessionAttributeListener#attributeRemoved(HttpSessionBindingEvent) */ public void attributeRemoved(HttpSessionBindingEvent arg0) { System.out.println("Session里面删除数据了"); } /** * @see HttpSessionAttributeListener#attributeReplaced(HttpSessionBindingEvent) */ public void attributeReplaced(HttpSessionBindingEvent arg0) { System.out.println("Session里面修改数据了"); } }
2)servlet
package com.anli; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * Servlet implementation class TestListenerServlet */ @WebServlet("/TestListenerServlet") public class TestListenerServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session=request.getSession(); session.setAttribute("a1", "增加数据"); session.setAttribute("a1", "修改数据"); session.removeAttribute("a1");//删除数据 } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
3)测试:直接启动该servlet,然后看console数据内容,会有以下的数据出现:
注意:其余两个我就不说明了,同样的套路,特别说明的一个点,就是查询数据的时候是不会触碰到这三个监听器的。
2)如果是额外的两个监听器的话
a.步骤:
(1)写好一个实类
(2)在该实类的基础之上实现某个接口
(3)实现对应的方法
b.触发条件:
案例三:使用HttpSessionBindingListener
1)对象类
package com.anli; import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionBindingListener; public class User implements HttpSessionBindingListener{ public String id; public String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public void valueBound(HttpSessionBindingEvent arg0) { System.out.println("该对象被绑定了"); } @Override public void valueUnbound(HttpSessionBindingEvent arg0) { System.out.println("该对象被取消绑定了"); } }
2)servlet
package com.anli; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * Servlet implementation class TestListenerServlet */ @WebServlet("/TestListenerServlet") public class TestListenerServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session=request.getSession(); // session.setAttribute("a1", "增加数据"); // session.setAttribute("a1", "修改数据"); // session.removeAttribute("a1");//删除数据 User user=new User(); session.setAttribute("a", user); session.removeAttribute("a"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
3)测试:直接运行该servlet,观察console:
每一章内容都是心血,希望大家不要抄袭~