目录
2.5与session中的绑定的对象相关的监听器(对象感知监听器)
一:什么是监听器?
监听器就是监听某个对象的的状态变化的组件
监听器的相关概念:
事件源:被监听的对象 ----- 三个域对象 request session servletContext
监听器:监听事件源对象 事件源对象的状态的变化都会触发监听器 ---- 6+2
注册监听器:将监听器与事件源进行绑定
响应行为:监听器监听到事件源的状态变化时 所涉及的功能代码
二 :监听器有哪些
2.1按维度划分
第一维度:按照被监听的对象划分:ServletRequest域 HttpSession域 ServletContext域
第二维度:监听的内容分:监听域对象的创建与销毁的 监听域对象的属性变 化的
2.2监听器的编写步骤:
-
编写一个监听器类去实现监听器接口
-
覆盖监听器的方法
-
需要在web.xml中进行配置---注册
2.3监听域对象创建与销毁
ServletContextListener
public class MyServletContextListener implements ServletContextListener{
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("servletContextListenner销毁了");
}
public void contextInitialized(ServletContextEvent sce) {
ServletContext servletContext = sce.getServletContext();
sce.getSource();
System.out.println("servletContextListenner创建了");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date firstTime = null;
try {
firstTime = format.parse("2018-12-10 20:37:46");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
System.out.println("银行计息了。。。。");
}
}, firstTime, 24*60*60*1000);
}
}
配置文件
<listener>
<listener-class>lx.test.Listener.MyServletContextListener</listener-class>
</listener>
HttpSessionListener、ServletRequestListener同上
2.4监听域对象属性变化
ServletContextAttributeListener
public class MyServletContextAttibuteListener implements ServletContextAttributeListener{
public void attributeAdded(ServletContextAttributeEvent scab) {
System.out.println("添加:"+scab.getName()+":"+scab.getValue());
}
public void attributeRemoved(ServletContextAttributeEvent scab) {
System.out.println("删除:"+scab.getName()+":"+scab.getValue());
}
public void attributeReplaced(ServletContextAttributeEvent scab) {
System.out.println("修改:"+scab.getName()+":"+scab.getValue());
}
}
配置文件
<listener>
<listener-class>lx.test.Listener.MyServletContextAttibuteListener</listener-class>
</listener>
测试类
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = this.getServletContext();
context.setAttribute("name", "lx");
context.setAttribute("name", "lx2");
context.removeAttribute("name");
}
结果
添加:name:lx
修改:name:lx
删除:name:lx2
HttpSessionAttributeListener、ServletRequestAriibuteListener同上
2.5与session中的绑定的对象相关的监听器(对象感知监听器)
绑定状态:就一个对象被放到session域中
解绑状态:就是这个对象从session域中移除了
钝化状态:是将session内存中的对象持久化(序列化)到磁盘
活化状态:就是将磁盘上的对象再次恢复到session内存中
绑定与解绑的监听器HttpSessionBindingListener
实体:
public class Person implements HttpSessionBindingListener{
private String id;
private String name;
public Person(String id, String name) {
super();
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void valueBound(HttpSessionBindingEvent hsb) {
System.out.println("Person被绑定了"+hsb.getName()+hsb.getValue());
}
public void valueUnbound(HttpSessionBindingEvent hsb) {
System.out.println("Person被解绑了");
}
}
测试类
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
Person person = new Person("1","lx");
session.setAttribute("person", person);
session.removeAttribute("person");
}
钝化与活化的监听器HttpSessionActivationListener
实体:
public class Customer implements HttpSessionActivationListener,Serializable{
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Customer(String id, String name) {
super();
this.id = id;
this.name = name;
}
public void sessionDidActivate(HttpSessionEvent hse) {
System.out.println("customer被活化了");
}
public void sessionWillPassivate(HttpSessionEvent hse) {
System.out.println("customer被钝化了");
}
}
测试类1:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
Customer customer = new Customer("1","lx");
session.setAttribute("customer", customer);
System.out.println("customer被放到session域中了");
}
测试类2;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Customer customer = (Customer) request.getSession().getAttribute("customer");
System.out.println(customer.getName());
}
配置文件:context.xml(放到WebContent下的META-INF目录下)
<Context>
<!-- maxIdleSwap:session中的对象多长时间不使用就钝化(分钟) -->
<!-- directory:钝化后的对象的文件写到磁盘的哪个目录下 配置钝化的对象文件在 work/catalina/localhost/钝化文件 -->
<Manager className="org.apache.catalina.session.PersistentManager" maxIdleSwap="1">
<Store className="org.apache.catalina.session.FileStore" directory="lxTestActivation" />
</Manager>
</Context>