监听器,是针对整个web环境的监听,
主要有以下三类:
1,ServletContext:Servlet上下文
2,Session:对Session监听
3,Request监听
一,对ServletContext监听
在web端实现监听=实现一系列的监听接口
1,ServletContextListener:对整个Servlet上下文的监听(启动,销毁)
public void contextInitialized(ServletContextEvent sce):上下文初始化
public void contextDestroyed(ServletContextEvent sce):上下文销毁
ServletContextEvent事件:取的一个ServletContext(application)对象
public ServletContext getServletContext()
2,ServletContextAttributeListener:对Servlet上下文属性的监听
public void attributeAdded(ServletContextAttributeEvent scab):增加属性
public void attributeRemoved(ServletContextAttributeEvent scab):属性删除
public void attributeRepalced(ServletContextAttributeEvent scab):属性替换(第二次设置同一属性)
ServletContextAttributeEvent事件:能取得设置属性的名称与内容
public String getName():得到属性名称
public Object getValue():去的属性的值
实例1:
对Servlet上下文的监听
package cn.lxh.listener;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class ServletContextDemo implements ServletContextListener,
ServletContextAttributeListener {
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("上下文销毁");
}
private ServletContext application=null;
public void contextInitialized(ServletContextEvent arg0) {
//ServletContextEvent能够得到一个ServletContext对象
this.application=arg0.getServletContext();
System.out.println("上下文初始化");
System.out.println("当前虚拟目录的绝对路径:"+this.application.getRealPath("/"));
}
public void attributeAdded(ServletContextAttributeEvent arg0) {
System.out.println("增加属性"+arg0.getName()+"-->"+arg0.getValue());
}
public void attributeRemoved(ServletContextAttributeEvent arg0) {
System.out.println("删除属性"+arg0.getName()+"-->"+arg0.getValue());
}
public void attributeReplaced(ServletContextAttributeEvent arg0) {
System.out.println("替换属性"+arg0.getName()+"-->"+arg0.getValue());
}
}
配置web.xml
<listener>
<listener-class>cn.lxh.listener.ServletContextDemo</listener-class>
</listener>
jsp页面demo.jsp
<% getServletContext().setAttribute("name","age"); %>
ServletContext监听器:表示监听整个web中ServletContext即(application)对象改变
二,对Session监听
对Session的创建,销毁,属性的监听操作
Session属于http协议下的内容:javax.servlet.http.*;包下
1,HttpSessionListener接口:对Session的整体状态的监听
public void sessionCreated(HttpSessionEvent se):session创建
public void sessionDestroyed(HttpSessionEvent se):session销毁
HttpSessionEvent事件:
public HttpSession getSession():取得当前操作的session
2,HttpSessionAttributeListener接口:对session的属性监听
public void attributeAdded(HttpSessionBindingEvent se):增加属性
public void attributeRemoved(HttpSessionBindingEvent se):删除属性
public void attributeReplaced(HttpSessionBindingEvent se):替换属性
HttpSessionBindingEvent事件:
public String getName():取得属性的名称
public Object getValue():取得属性的值
public HttpSession getSession():取得当前的session
对session的监听:
package cn.lxh.listener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class HttpSessionDemo implements HttpSessionListener,
HttpSessionAttributeListener {
private HttpSession session=null;
public void sessionCreated(HttpSessionEvent arg0) {
session=arg0.getSession();
System.out.println("获得增加session的ID:"+session.getId());
}
public void sessionDestroyed(HttpSessionEvent arg0) {
System.out.println("Session的销毁!");
}
public void attributeAdded(HttpSessionBindingEvent arg0) {
System.out.println("增加Session:"+arg0.getName()+"-->"+arg0.getValue());
}
public void attributeReplaced(HttpSessionBindingEvent arg0) {
System.out.println("替换Session:"+arg0.getName()+"-->"+arg0.getValue());
}
public void attributeRemoved(HttpSessionBindingEvent arg0) {
System.out.println("删除Session:"+arg0.getName()+"-->"+arg0.getValue());
}
}
Session的销毁有两种情况:
1,session超时;
在web.xml中配置
<listener>
<listener-class>cn.lxh.listener.HttpSessionDemo</listener-class>
</listener>
<session-config>
<!--失效时间-->
<session-timeout>1</session-timeout>
</session-config>
2,手工使session失效
invalidate():使session失效
在jsp页面上写入代码:
<% session.invalidate(); %>
监听器的应用:
统计当前在线人员列表
实现接口?
1,在线人员列表是对所有人都起作用,所有的数据必须保存在application之中,意味着代码中必须有一个ServletContext对象。
2,是针对Session的变化进行操作,如果登陆成功,则将用户名保存在session之中
3,如果用户注销,则应该将相应的额用户名称删除掉
package cn.lxh.listener;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.*;
public class OnLineDemo implements ServletContextListener, HttpSessionListener,HttpSessionAttributeListener{
public void contextDestroyed(ServletContextEvent arg0) {
}
private ServletContext apllication=null;
public void contextInitialized(ServletContextEvent arg0) {
this.apllication=arg0.getServletContext();
//在线列表是针对所有在线用户的,所以需要保存在application中
//但是所有的用户信息是一个动态变化的,因此将一个动态数组保存在application中
this.apllication.setAttribute("alluser", new ArrayList());
}
public void sessionCreated(HttpSessionEvent se) {
}
public void sessionDestroyed(HttpSessionEvent se) {
List l=(List)this.apllication.getAttribute("alluser");
String value=(String) se.getSession().getAttribute("uname");
l.remove(value);
this.apllication.setAttribute("alluser", l);
}
public void attributeAdded(HttpSessionBindingEvent se) {
List l=(List)this.apllication.getAttribute("alluser");
l.add(se.getValue());
this.apllication.setAttribute("alluser", l);
}
public void attributeRemoved(HttpSessionBindingEvent se) {
}
public void attributeReplaced(HttpSessionBindingEvent se) {
}
}