标签:iso https pre imp attribute sys 作用 event value
本节讲述常用监听器的用法。
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class MyServletContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("系统启动时执行");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("系统关闭时执行");
}
}
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
public class MyServletContextAttributeListener implements ServletContextAttributeListener {
@Override
public void attributeAdded(ServletContextAttributeEvent scab) {
System.out.println("在applicaiton作用域中add值时候触发");
}
@Override
public void attributeRemoved(ServletContextAttributeEvent scab) {
System.out.println("在applicaiton作用域中remove值时候触发");
}
@Override
public void attributeReplaced(ServletContextAttributeEvent scab) {
System.out.println("在applicaiton作用域中replace值时候触发");
}
}
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
public class MyHttpSessionBindingListener implements HttpSessionBindingListener {
@Override
public void valueBound(HttpSessionBindingEvent event) {
System.out.println("session.setAttribute触发该事件");
}
@Override
public void valueUnbound(HttpSessionBindingEvent event) {
System.out.println("session.removeAttribute触发该事件");
}
}
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
public class MyHttpSessionAttributeListener implements HttpSessionAttributeListener {
@Override
public void attributeAdded(HttpSessionBindingEvent se) {
System.out.println("在session作用域加入键值对触发");
}
@Override
public void attributeRemoved(HttpSessionBindingEvent se) {
System.out.println("在session作用域删除键值对触发");
}
@Override
public void attributeReplaced(HttpSessionBindingEvent se) {
System.out.println("在session作用域替换键值对触发");
}
}
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class MyHttpSessionListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent se) {
System.out.println("session被创建的时候触发");
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
System.out.println("session被销毁的时候触发");
}
}
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
public class MyServletRequestListener implements ServletRequestListener {
@Override
public void requestDestroyed(ServletRequestEvent sre) {
System.out.println("一次请求被销毁触发");
}
@Override
public void requestInitialized(ServletRequestEvent sre) {
System.out.println("一次请求初始化触发");
}
}
import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
public class MyServletRequestAttributeListener implements ServletRequestAttributeListener {
@Override
public void attributeAdded(ServletRequestAttributeEvent srae) {
System.out.println("request加入键值对触发");
}
@Override
public void attributeRemoved(ServletRequestAttributeEvent srae) {
System.out.println("request删除键值对触发");
}
@Override
public void attributeReplaced(ServletRequestAttributeEvent srae) {
System.out.println("request替换键值对触发");
}
}
import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionEvent;
public class MyHttpSessionActivationListener implements HttpSessionActivationListener {
@Override
public void sessionWillPassivate(HttpSessionEvent se) {
System.out.println("sesison被激活是触发");
}
@Override
public void sessionDidActivate(HttpSessionEvent se) {
System.out.println("session钝化是触发");
}
}
这就是监听器的基本用法。
标签:iso https pre imp attribute sys 作用 event value
原文地址:https://www.cnblogs.com/alichengxuyuan/p/12625390.html