8.3.4 HttpSessionBindingListener

8.3.4 HttpSessionBindingListener

当有属性绑定或者解绑到HttpSession上时,HttpSessionBindingListener监听器会被调用。如果对HttpSession属性的绑定和解绑动作感兴趣,就可以实现HttpSessionBindingListener来监听。例如可以在HttpSession属性绑定时更新状态,或者在属性解绑时释放资源。

实例

Product类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package app08a.model;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
public class Product
implements
HttpSessionBindingListener
{
private String id;
private String name;
private double price;
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 double getPrice()
{
return price;
}
public void setPrice(double price)
{
this.price = price;
}
@Override
public void valueBound(HttpSessionBindingEvent event)
{
String attributeName = event.getName();
System.out.println(attributeName + " valueBound");
}
@Override
public void valueUnbound(HttpSessionBindingEvent event)
{
String attributeName = event.getName();
System.out.println(attributeName + " valueUnbound");
}
}

这个监听器会在HttpSession属性绑定和解绑时在控制台打印信息。