Servlet3.0的注解自定义原生Listener监听器实战

时间:2024-09-14 20:05:26

简介:监听器介绍和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========"); } }

  Servlet3.0的注解自定义原生Listener监听器实战

  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========"); } }

Servlet3.0的注解自定义原生Listener监听器实战