分析

监听web对象创建与销毁的监听器 
  1. ServletContextListener
  2. HttpSessionListener
  3. ServletRequestListener
监听web对象属性变化的监听器 
  1. ServletContextAttributeListener
  2. HttpSessionAttributeListener
  3. ServletRequestAttributeListener
监听session绑定javaBean操作的监听器 
  1. HttpSessionBindingListener
  2. HttpSessionActivationListener
javaweb创建监听器的步骤:
1.创建一个类,实现指定的监听器接口
2.重写接口中的方法
3.在web.xml文件中配置监听器

实现

如下四种监听器为例:

ServletRequestListener

ServletRequestAttributeListener

HttpSessionBindingListener

打开被监听的网站。

监听器响应

HttpSessionActivationListener

代码

创建指定的监听器接口类并重写

ServletRequestListener

package listener01;
import java.util.Date;

import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
/*
 * 监听request创建和销毁
 * 
 */
public class RequestListnener implements ServletRequestListener{
	
	@Override//销毁request
	public void requestDestroyed(ServletRequestEvent sre) {
		System.out.println("==========监听request销毁情况==============");
		System.out.println("request被销毁"+new Date().toLocaleString());
	}
	@Override//监听request
	public void requestInitialized(ServletRequestEvent sre) {
		System.out.println("==========监听request创建情况==============");
		System.out.println("request被创建"+new Date().toLocaleString());
		System.out.println("  ");
	}
}

ServletRequestAttributeListener

package listener01;

import java.util.Date;

import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.annotation.WebListener;
/*
 * 
 * Request属性监听
 * 
 */

@WebListener
public class MyRequestAttributeListener implements ServletRequestAttributeListener {

	//删除一个Request属性
    public void attributeRemoved(ServletRequestAttributeEvent srae)  { 
         System.out.println("正在删除一个Request属性"+new Date().toLocaleString());
         System.out.println("属性名: "+srae.getName());
         System.out.println("属性值: "+srae.getValue());
 		System.out.println("  ");
    }
    //添加一个Request属性
    public void attributeAdded(ServletRequestAttributeEvent srae)  { 
       	System.out.println("==========监听request属性情况(增删改)================");
    	System.out.println("正在添加一个Request属性"+new Date().toLocaleString());
        System.out.println("属性名: "+srae.getName());
        System.out.println("属性值: "+srae.getValue());
    }

	//修改一个Request属性
    public void attributeReplaced(ServletRequestAttributeEvent srae)  { 
    	System.out.println("正在修改一个Request属性"+new Date().toLocaleString());
        System.out.println("属性名: "+srae.getName());
        System.out.println("属性值: "+srae.getValue());
    }
	
}

HttpSessionBindingListener

package listener01;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

/**
 * 监听session绑定javabean情况
 *
 */
@WebListener
public class people implements HttpSessionBindingListener {

	private String name;
	private int age;
	
	
    public void valueBound(HttpSessionBindingEvent event)  { 
    	 System.out.println("==========监听session绑定javabean情况==============");
        System.out.println("People被添加到Session中!");
     }

     public void valueUnbound(HttpSessionBindingEvent event)  { 
     	 System.out.println("People从Session中移除!");
     	 System.out.println("========================");
 		System.out.println("  ");
     }

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
 	
    }
	

HttpSessionActivationListener

package listener01;

import java.io.Serializable;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionEvent;

/**
 * session对象的活化与钝化
 *
 */
@WebListener
public class Student implements HttpSessionActivationListener,Serializable {
	private String name;
	private int age;
	
	

    public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public void sessionDidActivate(HttpSessionEvent se)  { 
      System.out.println("Session活化(恢复): "+se.getSession().getId());
    }

    public void sessionWillPassivate(HttpSessionEvent se)  { 
        System.out.println("Session钝化(保存): "+se.getSession().getId());
    }
	
}

在web.xml文件中配置监听器

index.jsp测试文件

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="listener01.people,listener01.Student" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
	request.setAttribute("Dongguan", "Rain");
	request.setAttribute("Dongguan", "Sunshine");
	request.removeAttribute("Dongguan"); 

    people p=new people();
  	session.setAttribute("people", p);
  	session.removeAttribute("people");
  	
  	Student s=new Student();
  	session.setAttribute("student", s);
  	
%>
</body>
</html>

注意事项

  1. web.xml路径格式:包名.监听器类名
  2. Session绑定javabean,session活化与钝化监听器不用再web.xml中加入监听字段
  3. session活化与钝化监听器需要的创建content.xml文档在META-INF文件夹下

web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>login</display-name>
<welcome-file-list>
  <welcome-file>index.html</welcome-file>
  <welcome-file>index.htm</welcome-file>
  <welcome-file>index.jsp</welcome-file>
  <welcome-file>default.html</welcome-file>
  <welcome-file>default.htm</welcome-file>
  <welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<listener>
  <listener-class>listener01.RequestListnener</listener-class>
</listener>
<listener>
  <listener-class>listener01.MyRequestAttributeListener</listener-class>
</listener>
</web-app>

content.xml

	<?xml version="1.0" encoding="UTF-8"?>
	<Context>
	    <Manager className="org.apache.catalina.session.PersistentManager" maxIdleSwap="1">
	    <Store className="org.apache.catalina.session.FileStore" directory="listener"/>
	    </Manager>
	</Context>