我们通常可以部署一些特殊的Servlet类来监听Web应用的上下文信息、Servlet会话信息,Servlet请求信息等。通过这些监听,我们可以自动执行某些程序。
例如,我们可以把利用监听会话信息,来统计在线人数。我们可以监听ServletContext,来初始化一些系统常量,初始化一些工厂等。我们可以通过对请求进行监听,可以控制用户访问,例如如果是本地机器访问,不用登陆,如果是远程机器访问,需要登陆。我们完全可以通过监听请求获取客户端地址,来做相应处理。
利用监听的实例比比皆是。
一、ServletContext监听,监听ServletContext信息。
1、实现ServletContextListener接口,监听ServletContext的创建和销毁
注:ServletContext初始化实在服务器启动时进行的,它的销毁实在服务器关闭时进行的。
- package com.jialin.drp.util;
- import javax.servlet.ServletContextEvent;
- import javax.servlet.ServletContextListener;
- public class MyServletContextListener implements ServletContextListener {
- @Override
- public void contextDestroyed(ServletContextEvent sce) {
- System.out.println("这是一个ServletContex监听");
- System.out.println("服务器即将关闭,保存系统数据...");
- }
- @Override
- public void contextInitialized(ServletContextEvent sce) {
- System.out.println("这是一个ServletContex监听");
- System.out.println("服务器启动,初始化系统参数...");
- }
- }
package com.jialin.drp.util;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class MyServletContextListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("这是一个ServletContex监听");
System.out.println("服务器即将关闭,保存系统数据...");
}
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("这是一个ServletContex监听");
System.out.println("服务器启动,初始化系统参数...");
}
}
2、实现ServletContextAttributeListener接口,监听ServletContext属性的增删改。
- package com.jialin.drp.util;
- import javax.servlet.ServletContextAttributeEvent;
- import javax.servlet.ServletContextAttributeListener;
- public class MyServletContextAttributeListener implements
- ServletContextAttributeListener {
- @Override
- public void attributeAdded(ServletContextAttributeEvent sce) {
- System.out.println("ServletContex增加属性");
- }
- @Override
- public void attributeRemoved(ServletContextAttributeEvent sce) {
- System.out.println("ServletContext删除属性");
- }
- @Override
- public void attributeReplaced(ServletContextAttributeEvent se) {
- System.out.println("ServletContex修改属性");
- }
- }
package com.jialin.drp.util;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
public class MyServletContextAttributeListener implements
ServletContextAttributeListener {
@Override
public void attributeAdded(ServletContextAttributeEvent sce) {
System.out.println("ServletContex增加属性");
}
@Override
public void attributeRemoved(ServletContextAttributeEvent sce) {
System.out.println("ServletContext删除属性");
}
@Override
public void attributeReplaced(ServletContextAttributeEvent se) {
System.out.println("ServletContex修改属性");
}
}
注:利用参数sce可以获取ServletContext的相关信息,并对其进行操作
二、监听HTTP会话信息
1、实现HttpSessionListener接口,监听Http会话创建,销毁信息
- package com.jialin.drp.util;
- import javax.servlet.http.HttpSessionEvent;
- import javax.servlet.http.HttpSessionListener;
- public class TestHttpSessionListener implements HttpSessionListener {
- public void sessionCreated(HttpSessionEvent sce) {
- System.out.println("创建Session");
- }
- public void sessionDestroyed(HttpSessionEvent sce) {
- System.out.println("销毁Session");
- }
- }
package com.jialin.drp.util;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class TestHttpSessionListener implements HttpSessionListener {
public void sessionCreated(HttpSessionEvent sce) {
System.out.println("创建Session");
}
public void sessionDestroyed(HttpSessionEvent sce) {
System.out.println("销毁Session");
}
}
2、实现HttpSessionAttributeListener接口,监听HTTP会话的属性设置请求
- package com.jialin.drp.util;
- import javax.servlet.http.HttpSessionAttributeListener;
- import javax.servlet.http.HttpSessionBindingEvent;
- public class TestHttpSessionAttributeListener implements
- HttpSessionAttributeListener {
- public void attributeAdded(HttpSessionBindingEvent sce) {
- System.out.println("增加属性");
- }
- public void attributeRemoved(HttpSessionBindingEvent sce) {
- System.out.println("删除属性");
- }
- public void attributeReplaced(HttpSessionBindingEvent sce) {
- System.out.println("修改属性");
- }
- }
package com.jialin.drp.util;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
public class TestHttpSessionAttributeListener implements
HttpSessionAttributeListener {
public void attributeAdded(HttpSessionBindingEvent sce) {
System.out.println("增加属性");
}
public void attributeRemoved(HttpSessionBindingEvent sce) {
System.out.println("删除属性");
}
public void attributeReplaced(HttpSessionBindingEvent sce) {
System.out.println("修改属性");
}
}
3、通过实现HttpSessionActivationListener接口监听Http会话的active,passivate情况,通过实现HttpSessionBingdingListener监听Http会话中对象的绑定信息也与上述类似,这里不再赘述。同样,通过sce参数可以拿到Http会话的相关信息进行操作
三、Servlet请求监听
1、实现ServletRequestListener,监听请求的创建和销毁
- package com.jialin.drp.util;
- import javax.servlet.ServletRequestEvent;
- import javax.servlet.ServletRequestListener;
- public class TestServletRequestListener implements ServletRequestListener {
- @Override
- public void requestDestroyed(ServletRequestEvent sre) {
- System.out.println("请求销毁");
- }
- @Override
- public void requestInitialized(ServletRequestEvent sre) {
- System.out.println("开始请求");
- }
- }
package com.jialin.drp.util;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
public class TestServletRequestListener implements ServletRequestListener {
@Override
public void requestDestroyed(ServletRequestEvent sre) {
System.out.println("请求销毁");
}
@Override
public void requestInitialized(ServletRequestEvent sre) {
System.out.println("开始请求");
}
}
2、实现ServletRequestAttrivuteListener,监听请求属性的更改
- package com.jialin.drp.util;
- import javax.servlet.ServletRequestAttributeEvent;
- import javax.servlet.ServletRequestAttributeListener;
- public class TestServletRequestAttributeListener implements
- ServletRequestAttributeListener {
- @Override
- public void attributeAdded(ServletRequestAttributeEvent sre) {
- System.out.println("增加属性");
- }
- @Override
- public void attributeRemoved(ServletRequestAttributeEvent sre) {
- System.out.println("删除属性");
- }
- @Override
- public void attributeReplaced(ServletRequestAttributeEvent sre) {
- System.out.println("修改属性");
- }
- }
package com.jialin.drp.util;
import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
public class TestServletRequestAttributeListener implements
ServletRequestAttributeListener {
@Override
public void attributeAdded(ServletRequestAttributeEvent sre) {
System.out.println("增加属性");
}
@Override
public void attributeRemoved(ServletRequestAttributeEvent sre) {
System.out.println("删除属性");
}
@Override
public void attributeReplaced(ServletRequestAttributeEvent sre) {
System.out.println("修改属性");
}
}
同样的,通过参数sre,我们可以取得请求的相关信息来进行相关的操作
最后不要忘记,要把监听加到配置文件web.xml中
<listener>
<listener-class>完整类路径</listener-class>
</listener>
例如
<listener>
<listener-class>com.jialin.drp.util.TestHttpSessionListener</listener-class>
</listener>