Servlet3.0的注解自定义原生Listener监听器实战
简介:监听器介绍和Servlet3.0的注解自定义原生Listener监听器实战
自定义Listener(常用的监听器 servletContextListener、httpSessionListener、servletRequestListener)
代码示例:
RequestListener.java
1 package net.xdclass.demo.listener; 2 3 import javax.servlet.ServletContextEvent; 4 import javax.servlet.ServletContextListener; 5 import javax.servlet.annotation.WebListener; 6 7 @WebListener 8 public class CustomContextListener implements ServletContextListener{ 9 10 //主要作为资源加载 11 @Override 12 public void contextInitialized(ServletContextEvent sce) { 13 System.out.println("======contextInitialized========"); 14 15 } 16 17 @Override 18 public void contextDestroyed(ServletContextEvent sce) { 19 System.out.println("======contextDestroyed========"); 20 21 } 22 23 }
CustomContextListener.java:
1 package net.xdclass.demo.listener; 2 3 import javax.servlet.ServletRequestEvent; 4 import javax.servlet.ServletRequestListener; 5 import javax.servlet.annotation.WebListener; 6 7 @WebListener 8 public class RequestListener implements ServletRequestListener { 9 10 //主要作为统计 11 @Override 12 public void requestDestroyed(ServletRequestEvent sre) { 13 // TODO Auto-generated method stub 14 System.out.println("======requestDestroyed========"); 15 } 16 17 @Override 18 public void requestInitialized(ServletRequestEvent sre) { 19 System.out.println("======requestInitialized========"); 20 21 } 22 23 24 }