|
ServletContextListener监听接口:
package cn.tty.demo.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class ServletContextListenerDemo01 implements ServletContextListener{
//ServletContext(application)范围内的容器初始化和销毁的监听
public void contextDestroyed(ServletContextEvent event) {
System.out.println("容器初始化-->"+event.getServletContext().getContextPath());
}
public void contextInitialized(ServletContextEvent event) {
System.out.println("容器创建-->"+event.getServletContext().getContextPath());
try{
Thread.sleep(500);//休眠
}catch(Exception e){
e.printStackTrace();
}
}
}
ServletContextAttributeListener监听接口:
package cn.tty.demo.listener;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
public class ServletContextAttributeListenerDemo01implements
ServletContextAttributeListener {
public void attributeAdded(ServletContextAttributeEvent event) {
System.out.println("application范围属性被添加,属性名称:"+
event.getName()+",属性值:"+event.getValue());
}
public void attributeRemoved(ServletContextAttributeEvent event) {
System.out.println("application范围属性被删除,属性名称:"+
event.getName()+",属性值:"+event.getValue());
}
public void attributeReplaced(ServletContextAttributeEvent event) {
System.out.println("application范围属性被替换,属性名称:"+
event.getName()+",属性值:"+event.getValue());
}
}
<%@page language="java" import="java.util.*" pageEncoding="GBK"%>
<HTML>
<head><title>ServletContextAttributeListenerDemo01</title></head>
<body>
<%
this.getServletContext().setAttribute("name","tty");
this.getServletContext().setAttribute("name","hyl");
this.getServletContext().removeAttribute("name");
%>
</body>
</html> |
|