監聽器(對application監聽)
1:jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
<title>對Application監聽</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<%
this.getServletContext().setAttribute("info", "監聽器");
%>
<%
this.getServletContext().removeAttribute("info");
%>
</body>
</html>
2:Listener(監聽器)
package cn.com.filter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletContextAttributeListenerDemo implements
ServletContextAttributeListener {
@Override
public void attributeAdded(ServletContextAttributeEvent event) {
System.out.println("**增加屬性-->屬性名稱:"+event.getName()+",屬性內容:"+event.getValue());
}
@Override
public void attributeReplaced(ServletContextAttributeEvent event) {
System.out.println("**替換屬性屬性-->屬性名稱:"+event.getName()+",屬性內容:"+event.getValue());
}
@Override
public void attributeRemoved(ServletContextAttributeEvent event) {
System.out.println("**刪除屬性-->屬性名稱:"+event.getName()+",屬性內容:"+event.getValue());
}
}