监听对象
request:ServletRequestListener
session:HttpSessionListener
application:ServletContextListener
每个监听器各自都提供了2个方法:监听开始和监听结束
代码示例:
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class ContextSessionRequestListener implements ServletRequestListener, HttpSessionListener, ServletContextListener {
//application(ServletContext对象)
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("监听ServletContext,创建ServletContext对象:" + sce);
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("监听ServletContext,销毁ServletContext对象:" + sce);
}
//监听ServletRequest对象
@Override
public void requestDestroyed(ServletRequestEvent sre) {
System.out.println("监听ServletRequest,创建ServletRequest对象:" + sre);
}
@Override
public void requestInitialized(ServletRequestEvent sre) {
System.out.println("监听ServletRequest,销毁ServletRequest对象:" + sre);
}
//监听HttpSession对象
@Override
public void sessionCreated(HttpSessionEvent se) {
System.out.println("监听HttpSession,创建HttpSession对象:" + se);
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
System.out.println("监听HttpSession,销毁HttpSession对象:" + se);
}
}
web.xml配置
<!--监听器配置-->
<listener>
<listener-class>com.xc.listener.ContextSessionRequestListener</listener-class>
</listener>
测试截图:
ServletContext在servlet容器启动时自动创建
监听对象中属性的变更:
- request: ServletRequestAttributeListener
- session:HttpSessionAttributeListener
- application:ServletContextAttributeListener
代码示例:
package com.xc.blog.listener;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
public class AttributeListener implements ServletRequestAttributeListener, HttpSessionAttributeListener, ServletContextAttributeListener {
@Override
public void attributeAdded(ServletContextAttributeEvent scab) {
String attrName = scab.getName();
Object attrValue = scab.getServletContext().getAttribute(attrName);
System.out.println("ServletContext【增加】属性:属性名" + attrName + ",属性值" + attrValue);
}
@Override
public void attributeRemoved(ServletContextAttributeEvent scab) {
System.out.println("ServletContext【删除】属性:属性名" + scab.getName());
}
@Override
public void attributeReplaced(ServletContextAttributeEvent scab) {
/**
* application.setAttribute("name"."zs");--新增name属性
* application.setAttribute("name"."ls");--替换name属性
* */
String attrName = scab.getName();
Object attrValue = scab.getServletContext().getAttribute(attrName);
System.out.println("ServletContext【替换】属性:属性名" + scab.getName()+ ",属性值" + attrValue);
}
@Override
public void attributeAdded(ServletRequestAttributeEvent srae) {
String attrName = srae.getName();
Object attrValue = srae.getServletContext().getAttribute(attrName);
System.out.println("(ServletRequest【增加】属性:属性名" + attrName + ",属性值" + attrValue);
}
@Override
public void attributeRemoved(ServletRequestAttributeEvent srae) {
System.out.println("(ServletRequest【删除】属性:属性名" + srae.getName());
}
@Override
public void attributeReplaced(ServletRequestAttributeEvent srae) {
String attrName = srae.getName();
Object attrValue = srae.getServletContext().getAttribute(attrName);
System.out.println("(ServletRequest【替换】属性:属性名" + srae.getName()+ ",属性值" + attrValue);
}
@Override
public void attributeAdded(HttpSessionBindingEvent se) {
String attrName = se.getName();
Object attrValue = se.getSession().getAttribute(attrName);
System.out.println("HttpSession【增加】属性:属性名" + attrName + ",属性值" + attrValue);
}
@Override
public void attributeRemoved(HttpSessionBindingEvent se) {
System.out.println("HttpSession【删除】属性:属性名" +se.getName());
}
@Override
public void attributeReplaced(HttpSessionBindingEvent se) {
String attrName = se.getName();
Object attrValue = se.getSession().getAttribute(attrName);
System.out.println("HttpSession【替换】属性:属性名" +se.getName()+ ",属性值" + attrValue);
}
}
测试截图:
监听器的开发步骤:
- 编写监听器,实现接口
- 配置web.xml
- 测试使用