监听器:监听对象的状态变化的组件。
监听器的相关要素:
1.事件源:被监听的对象,如request,session,servletContext
2.监听器:监听时间源对象,事件源对象的状态的变化都会触发监听器
3.注册监听器:将监听器与事件源进行绑定
4.响应行为:监听器监听到事件源的状态变化时,所触发的相关行为。
监听器的划分:
1.按照被监听的对象:
a.ServletRequest
b.HttpSession
c.ServletContext
2.按照监听的状态分:
a.监听域对象的创建与销毁
b.监听域对象的属性变化
监听器demo:
1.创建自定义监听器,实现ServletContextListener接口
public class MyServletContextListener implements ServletContextListener {
/**
* Default constructor.
*/
public MyServletContextListener() {
}
/**
* @see ServletContextListener#contextDestroyed(ServletContextEvent)
*/
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("contextDestroyed...");
}
/**
* @see ServletContextListener#contextInitialized(ServletContextEvent)
*/
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("contextInitialized...");
}
}
2.在web.xml中配置监听器
com.milan.listener.MyServletContextListener
a.对象的初始化,如加载数据库驱动,连接池等
b.配置文件的初始化,如spring的相关配置文件
c.定时器,如Timer/TimerTask
属性监听器demo:
创建自定义的属性监听器,实现ServletContextAttributeListener
public class MyServletContextAttributeListener implements ServletContextAttributeListener {
/**
* Default constructor.
*/
public MyServletContextAttributeListener() {
}
/**
* @see ServletContextAttributeListener#attributeAdded(ServletContextAttributeEvent)
*/
public void attributeAdded(ServletContextAttributeEvent arg0) {
System.out.println("attributeAdded...");
System.out.println("attributeAdded..."+arg0.getName());
System.out.println("attributeAdded..."+arg0.getValue());
}
/**
* @see ServletContextAttributeListener#attributeRemoved(ServletContextAttributeEvent)
*/
public void attributeRemoved(ServletContextAttributeEvent arg0) {
System.out.println("attributeRemoved。。。");
}
/**
* @see ServletContextAttributeListener#attributeReplaced(ServletContextAttributeEvent)
*/
public void attributeReplaced(ServletContextAttributeEvent arg0) {
System.out.println("attributeReplaced...");
}
}
web.xml中配置属性监听器:
com.milan.listener.MyServletContextAttributeListener
public class TestServletContextAttributeListener extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public TestServletContextAttributeListener() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getServletContext().setAttribute("name", "kaka");
request.getServletContext().setAttribute("name", "milan");
request.getServletContext().removeAttribute("name");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
控制台输出如下:
attributeAdded...
attributeAdded...name
attributeAdded...kaka
attributeReplaced...name
attributeRemoved...name
绑定解绑监听器:此监听器无需再web.xml中进行配置
Person类上绑定HttpSessionBindingListener
public class Person implements HttpSessionBindingListener{
private String username;
private int age;
public Person(String username, int age) {
super();
this.username = username;
this.age = age;
}
@Override
public String toString() {
return "Person [username=" + username + ", age=" + age + "]";
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public void valueBound(HttpSessionBindingEvent arg0) {
System.out.println("person:valueBound");
}
@Override
public void valueUnbound(HttpSessionBindingEvent arg0) {
System.out.println("person:valueUnbound");
}
}
测试监听器的绑定与解绑的类TestPersonBinding
public class TestPersonBinding extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public TestPersonBinding() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
Person p = new Person("milan", 1988);
session.setAttribute("person", p);
session.removeAttribute("person");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
运行后,测试结果如下:控制台打印了
person:valueBound
person:valueUnbound
钝化活化监听器:
创建Student类,实现Serializable,HttpSessionActivationListener
public class Student implements Serializable,HttpSessionActivationListener{
private String username;
private int age;
public Student(String username, int age) {
super();
this.username = username;
this.age = age;
}
@Override
public String toString() {
return "Student [username=" + username + ", age=" + age + "]";
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public void sessionDidActivate(HttpSessionEvent arg0) {
System.out.println("Student:活化");
}
@Override
public void sessionWillPassivate(HttpSessionEvent arg0) {
System.out.println("Student:钝化");
}
}
创建测试类TestStudentActivation
public class TestStudentActivation extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public TestStudentActivation() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
Student s = new Student("milan", 1988);
session.setAttribute("student", s);
System.out.println("student存入session");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
运行Tomcat:控制台输出如下
contextInitialized...
地址栏输入:http://localhost:8081/Tomcat/TestStudentActivation,控制台输出如下
student存入session
关闭tomcat,控制台输出如下
Student:钝化
contextDestroyed...
启动tomcat:控制台输出如下
contextInitialized...
Student:活化
此时可以访问session中的Student的相关属性