简介:监听器介绍和Servlet3.0的注解自定义原生Listener监听器实战
自定义Listener(常用的监听器 servletContextListener、httpSessionListener、servletRequestListener)
代码示例:
RequestListener.java
package net.xdclass.demo.listener; import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener; @WebListener
public class CustomContextListener implements ServletContextListener{ //主要作为资源加载
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("======contextInitialized========"); } @Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("======contextDestroyed========"); } }
CustomContextListener.java:
package net.xdclass.demo.listener; import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener; @WebListener
public class RequestListener implements ServletRequestListener { //主要作为统计
@Override
public void requestDestroyed(ServletRequestEvent sre) {
// TODO Auto-generated method stub
System.out.println("======requestDestroyed========");
} @Override
public void requestInitialized(ServletRequestEvent sre) {
System.out.println("======requestInitialized========"); } }