1 ServletContextListener
监听ServletContext对象被创建和销毁的Servlet监听器
开发:>创建一个实现了ServletContextListener的类并且实现里面的两个方法
public class HelloServletContextListener implements ServletContextListener
public class HelloServletContextListener implements ServletContextListener { public void contextDestroyed(ServletContextEvent sce) { // TODO Auto-generated method stub System.out.println("销毁"); } public void contextInitialized(ServletContextEvent sce) { // TODO Auto-generated method stub System.out.println("创建"); }
>并在web.xml里配置该Listener
<!-- 配置listener --> <listener> <listener-class>cn.stud.wlc.listener.HelloServletContextListener</listener-class> </listener>
>ServletContextListener最常用在web应用加载时对当前web应用的相关资源进行初始化操作(创建数据库链接池。。。)
(2,3)ServletRequestListener& HttpSessionListener和ServletContextListener类似
public class HelloServletContextListener implements ServletContextListener,HttpSessionListener,ServletRequestListener { public void contextDestroyed(ServletContextEvent sce) { // TODO Auto-generated method stub System.out.println("销毁"); } public void contextInitialized(ServletContextEvent sce) { // TODO Auto-generated method stub System.out.println("创建"); } public void requestDestroyed(ServletRequestEvent sre) { // TODO Auto-generated method stub } public void requestInitialized(ServletRequestEvent sre) { // TODO Auto-generated method stub } public void sessionCreated(HttpSessionEvent se) { // TODO Auto-generated method stub } public void sessionDestroyed(HttpSessionEvent se) { // TODO Auto-generated method stub } }
>request:是一个请求,当一个响应返回时,即被销毁,当发送一个请求时被创建。(注意请求转发是一个request)重定向是两个请求
>session:当第一次访问web应用的jsp或servlet时,且该servlet或jsp还需要创建session时(session=true时),此时会服务器创建session,浏览器未关(session未过期) 再次访问其他页面时session不会再次创建——session销毁:session过期,直接调用invalid方法,当前web应用被卸载(session可以被持久化)关闭浏览器并不意 味着session被销毁还可以通过sessionid找到服务器的session。
http://localhost:8080/day_01_22/FontFilter.jsp;jsessionid=DD05FBD13644D0C53959B23E347DB082
>application:贯穿一当前web应用的生命周期当前应用被加载时创建application对象,当前web应用被卸载时销毁application对象
域对象中属性变更的事件监听器
public class TestAttributeListener implements HttpSessionAttributeListener,ServletContextAttributeListener,ServletRequestAttributeListener { 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中替换了属性"); } public void attributeAdded(ServletContextAttributeEvent scab) { // TODO Auto-generated method stub } public void attributeRemoved(ServletContextAttributeEvent scab) { // TODO Auto-generated method stub } public void attributeReplaced(ServletContextAttributeEvent scab) { // TODO Auto-generated method stub } public void attributeAdded(HttpSessionBindingEvent se) { // TODO Auto-generated method stub } public void attributeRemoved(HttpSessionBindingEvent se) { // TODO Auto-generated method stub } public void attributeReplaced(HttpSessionBindingEvent se) { // TODO Auto-generated method stub } }
jsp
<body> <% request.setAttribute("name", "ABCD"); System.out.println("--------------------"); request.setAttribute("name", "EFGH"); System.out.println("--------------------"); request.removeAttribute("name"); System.out.println("--------------------"); %> </body>
运行结果
HttpSessionBindingListener(不需要再web.xml中配置)API getName(),getValue(),getSession()
public class BindingListenerBean implements HttpSessionBindingListener { public void valueBound(HttpSessionBindingEvent event) { System.out.println("绑定到session"); } public void valueUnbound(HttpSessionBindingEvent event) { System.out.println("从session解除绑定"); }
jsp
<h4>BindingListener</h4> <!-- http://localhost:8080/vip/Listener/HttpSessionBindingListener.jsp --> <% BindingListenerBean BLB = new BindingListenerBean(); session.setAttribute("BLB", BLB); System.out.println("--------------------"); session.removeAttribute("BLB"); %>
结果