ServletContextListener
ServletContext对象创建和销毁时触发的监听器
该对象有两个方法,分别是contextInitialized() 和contextDestroyed() 对应着ServletContext对象的创建与销毁。一个ServletContext可以有多个listener
ServletContext
一个web应用对应着一个ServletContext,ServletContext的根目录为当前web应用的webRoot目录
public class FirstListener implements ServletContextListener {
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("###contextDestroy");
}
public void contextInitialized(ServletContextEvent arg0) {
ServletContext context = arg0.getServletContext();
for(Object temp : context.getResourcePaths("/")){
System.out.println(temp);
}
}
}
结果是:
/index.jsp
/WEB-INF/
/META-INF/
配置监听器
在web.xml中添加一下内容:
<listener>
<listener-class>com.twilight.listener.FirstListener</listener-class>
</listener>
HttpSessionListener
在浏览器中访问http://localhost:8080/FirstFilter 时会创建session,tomcat会输出sessionCreated,一段时间后session会自动失效,然后tomcat会输出sessionDestroyed
一个Session对应多个listener
public void sessionCreated(HttpSessionEvent arg0) {
System.out.println("sessionCreated");
}
public void sessionDestroyed(HttpSessionEvent arg0) {
System.out.println("sessionDestroyed");
}
控制失效时间
在web.xml 中配置session-timeout 以分钟为单位。当时间是0或复数时表示永久存在。
<session-config>
<session-timeout>1</session-timeout>
</session-config>
配置监听器
在web.xml中添加一下内容:
<listener>
<listener-class>com.twilight.listener.MyHttpSessionListener</listener-class>
</listener>
作用
- 统计在线人数
- 记录访问日志
ServletRequestListener
表示用户请求的监听
两个方法:requestInitialized() 和requestDestroyed()
public void requestDestroyed(ServletRequestEvent arg0) {
System.out.println("requestDestroyed");
}
public void requestInitialized(ServletRequestEvent arg0) {
System.out.println("requestInitialized");
}
结果是:
requestInitialized
sessionCreated
requestDestroyed
配置监听器
在web.xml中添加一下内容:
<listener>
<listener-class>com.twilight.listener.MyServletRequestListener</listener-class>
</listener>
作用
- 读取参数
- 记录访问历史
对象中增加和删除属性的事件监听器
三个事件都有对应的属性监听器:
- ServletContext — ServletContextAttributeListener
- HttpSession — HttpSessionAttributeListener
- ServletRequest — ServletRequestAttributeListener
每个属性监听器都有三个方法:
- attributeAdded () 监听属性的增加
- attributeRemoved() 监听属性的移除
- attributeReplaced() 监听属性的替换
ServletRequestAttributeListener 代码如下,其他监听器代码类似:
public class MyServletRequestAttributeListener implements
ServletRequestAttributeListener {
public void attributeAdded(ServletRequestAttributeEvent arg0) {
System.out.println("ServletRequestAttributeListener_attributeAdded_"+arg0.getName());
}
public void attributeRemoved(ServletRequestAttributeEvent arg0) {
System.out.println("ServletRequestAttributeListener_attributeRemoved_"+arg0.getName());
}
public void attributeReplaced(ServletRequestAttributeEvent arg0) {
System.out.println("ServletRequestAttributeListener_attributeReplaced_"+arg0.getName());
}
}
addAttribute.jsp模拟增加属性
request.setAttribute("ServletRequest", "ServletRequestValue");
session.setAttribute("Session", "SessionValue");
session.getServletContext().setAttribute("ServletContext", "ServletContextValue");
removeAttribute.jsp 模拟移除属性
request.removeAttribute("ServletRequest");
session.removeAttribute("Session");
session.getServletContext().removeAttribute("ServletContext");
第一次访问addAttribute.jsp:
requestInitialized
ServletRequestAttributeListener_attributeReplaced_org.apache.catalina.ASYNC_SUPPORTED
ServletRequestAttributeListener_attributeAdded_ServletRequest
HttpSessionAttributeListener_attributeAdded_Session
ServletContextAttributeListener_attributeAdded_ServletContext
requestDestroyed
重复访问,发现只有Request是add,因为之前的Request已经失效,此时的Request是新创建的对象:
requestInitialized
ServletRequestAttributeListener_attributeReplaced_org.apache.catalina.ASYNC_SUPPORTED
ServletRequestAttributeListener_attributeAdded_ServletRequest
HttpSessionAttributeListener_attributeReplaced_Session
ServletContextAttributeListener_attributeReplaced_ServletContext
requestDestroyed
访问removeAttribute.jsp,此时之前的Request对象已经被销毁,所以不会看到request被remove
requestInitialized
ServletRequestAttributeListener_attributeReplaced_org.apache.catalina.ASYNC_SUPPORTED
HttpSessionAttributeListener_attributeRemoved_Session
ServletContextAttributeListener_attributeRemoved_ServletContext
requestDestroyed
HttpSession 中对象状态(绑定、解除)
- 绑定 :将对象保存到session中
- 解除 :从session中移除对象
HttpSessionBindingListener
该类类似一个javabean保存一些需要的信息,它有两个必须的方法:
- valueBound() 绑定
- valueUnbound() 解绑定
package com.twilight.entity;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
public class User implements HttpSessionBindingListener {
private String userName ;
private String password ;
public void valueBound(HttpSessionBindingEvent arg0) {
System.out.println("HttpSessionBindingListener_valueBound_"+arg0.getName());
}
public void valueUnbound(HttpSessionBindingEvent arg0) {
System.out.println("HttpSessionBindingListener_valueUnbound+"+arg0.getName());
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
该类不需要注册,可以直接使用,因为他可以认为只是一个继承了HttpSessionBindingListener接口的Javabean,使用方法:
session.setAttribute("userObj", new User());
session.removeAttribute("userObj");
结果是:
// 绑定
requestInitialized
HttpSessionBindingListener_valueBound_userObj
requestDestroyed
// 解绑定
requestInitialized
HttpSessionBindingListener_valueUnbound+userObj
requestDestroyed
钝化和活化
- 钝化 将session序列化到磁盘
- 活化 将序列化的对象反序列化到内存中
什么时候钝化
- 当tomcat服务器关闭或重启时,会将当前的session对象钝化
- 当web应用被重新加载时(reload)钝化
当tomcat服务器重启开启时,重新加载文件道内存并删除本地文件
HttpSessionActivationListener
实现钝化和话的接口,具体方法有两个:
- sessionWillPassivate 钝化
- sessionDidActivate 活化
由于钝化需要将对象序列化,所以还需要实现Serializable接口。代码如下:
package com.twilight.entity;
import java.io.Serializable;
import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import javax.servlet.http.HttpSessionEvent;
public class User implements HttpSessionBindingListener ,HttpSessionActivationListener ,Serializable {
private String userName ;
private String password ;
public void valueBound(HttpSessionBindingEvent arg0) {
System.out.println("HttpSessionBindingListener_valueBound_"+arg0.getName());
}
public void valueUnbound(HttpSessionBindingEvent arg0) {
System.out.println("HttpSessionBindingListener_valueUnbound+"+arg0.getName());
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void sessionDidActivate(HttpSessionEvent se) {
System.out.println("HttpSessionActivationListener_sessionDidActivate_"+se.getSource());
}
public void sessionWillPassivate(HttpSessionEvent se) {
System.out.println("HttpSessionActivationListener_sessionWillPassivate_"+se.getSource());
}
}
结果:
// 钝化
HttpSessionActivationListener_sessionWillPassivate_org.apache.catalina.session.StandardSessionFacade@720a366b
// 活化
HttpSessionActivationListener_sessionDidActivate_org.apache.catalina.session.StandardSessionFacade@798aa3f4
钝化后的文件保存在 Tomcat\work\Catalina\localhost\项目名\SESSIONS.ser文件中,服务器启动后文件被反序列化,然后被删除