1 16 17 package listeners; 18 19 20 import javax.servlet.ServletContext ; 21 import javax.servlet.ServletContextAttributeEvent ; 22 import javax.servlet.ServletContextAttributeListener ; 23 import javax.servlet.ServletContextEvent ; 24 import javax.servlet.ServletContextListener ; 25 26 27 36 37 public final class ContextListener 38 implements ServletContextAttributeListener , ServletContextListener { 39 40 41 43 44 47 private ServletContext context = null; 48 49 50 52 53 58 public void attributeAdded(ServletContextAttributeEvent event) { 59 60 log("attributeAdded('" + event.getName() + "', '" + 61 event.getValue() + "')"); 62 63 } 64 65 66 71 public void attributeRemoved(ServletContextAttributeEvent event) { 72 73 log("attributeRemoved('" + event.getName() + "', '" + 74 event.getValue() + "')"); 75 76 } 77 78 79 84 public void attributeReplaced(ServletContextAttributeEvent event) { 85 86 log("attributeReplaced('" + event.getName() + "', '" + 87 event.getValue() + "')"); 88 89 } 90 91 92 97 public void contextDestroyed(ServletContextEvent event) { 98 99 log("contextDestroyed()"); 100 this.context = null; 101 102 } 103 104 105 110 public void contextInitialized(ServletContextEvent event) { 111 112 this.context = event.getServletContext(); 113 log("contextInitialized()"); 114 115 } 116 117 118 120 121 126 private void log(String message) { 127 128 if (context != null) 129 context.log("ContextListener: " + message); 130 else 131 System.out.println("ContextListener: " + message); 132 133 } 134 135 136 143 private void log(String message, Throwable throwable) { 144 145 if (context != null) 146 context.log("ContextListener: " + message, throwable); 147 else { 148 System.out.println("ContextListener: " + message); 149 throwable.printStackTrace(System.out); 150 } 151 152 } 153 154 155 } 156 | Popular Tags |