1
2
3
4
5
6
7
8
9
10
11
|
package
listener;
import
javax.servlet.ServletContextEvent;
import
javax.servlet.ServletContextListener;
public
class
ServletContextListenerDemo
implements
ServletContextListener{
public
void
contextInitialized(ServletContextEvent event){
//对上下文初始化触发
System.out.println(
"**容器初始化-->"
+event.getServletContext().getContextPath());
}
public
void
contextDestroyed(ServletContextEvent event){
//上下文销毁时触发
System.out.println(
"**容器销毁-->"
+event.getServletContext().getContextPath());
}
}zai
|
1
2
3
4
5
|
<listener>
<listener-
class
>
listener.ServletContextListenerDemo
</listener-
class
>
</listener>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package
listener;
import
javax.servlet.*;
public
class
ServletContextAttributeListenerDemo
implements
ServletContextAttributeListener {
public
void
attributeAdded(ServletContextAttributeEvent event){
//属性增加时触发
System.out.println(
"**增加属性->:"
+event.getName()+
",属性内容:"
+event.getValue());
}
public
void
attributeRemoved(ServletContextAttributeEvent event){
//属性删除的时触发
System.out.println(
"**删除属性->属性名称:"
+event.getName()+
",属性内容:"
+event.getValue());
}
public
void
attributeReplaced(ServletContextAttributeEvent event){
//属性替换时触发
System.out.println(
"**增加替换->属性名称:"
+event.getName()+
",属性内容:"
+event.getValue());
}
}
|
1
2
3
4
5
|
<listener>
<listener-
class
>
listener.ServletContextAttributeListenerDemo
</listener-
class
>
</listener>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
application_attribute_add.jsp
<%@ page language=
"java"
contentType=
"text/html"
pageEncoding=
"utf-8"
%>
<html>
<head>
<title>WEB开发项目</title>
</head>
<body>
<%
//设置application范围属性
this
.getServletContext().setAttribute(
"info"
,
"http://zhaoyuqiang.blog.51cto.com"
);
%>
</body>
</html>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
application_attribute_remove.jsp
<%@ page language=
"java"
contentType=
"text/html"
pageEncoding=
"utf-8"
%>
<html>
<head>
<title>WEB开发项目</title>
</head>
<body>
<%
//设置application范围属性
this
.getServletContext().removeAttribute(
"info"
);
%>
</body>
</html>
|