一、监听三个域对象创建和销毁的事件监听器
public class MyServletContextListener implements ServletContextListener { public void contextDestroyed(ServletContextEvent arg0) { System.out.println(arg0.getServletContext() + "被创建了"); } public void contextInitialized(ServletContextEvent arg0) { System.out.println(arg0.getServletContext() + "被销毁了"); } }在web.xml文件中配置
<listener> <listener-class>cn.class3g.web.listener.MyServletContextListener </listener-class> </listener>执行结果为:启动服务器时打印”被创建了“停止服务器时打印“被销毁了”;
public class MyHttpSessionListener implements HttpSessionListener { public void sessionCreated(HttpSessionEvent arg0) { System.out.println(arg0.getSource() + "被销毁了"); } public void sessionDestroyed(HttpSessionEvent arg0) { System.out.println(arg0.getSource() + "被创建了"); } }
在web.xml文件中配置
<listener> <listener-class>cn.csdn.web.listener.MyHttpSessionListener</listener-class> </listener>
执行结果为:访问index.jsp时被创建了而且访问index.html不会被访问,因为如jsp页面中的page命令中有session="true"如果设置为"false"时就不会被创建了;
当刷新jsp页面时会再次创建再次打印;关闭浏览器时不会销毁,因为session默认存在30分钟;
HttpSessionListener的作用:统计在线人数
创建类MyServletRequestListener实现ServletRequestListener
public class MyServletRequestListener implements ServletRequestListener { public void requestDestroyed(ServletRequestEvent arg0) { System.out.println("请求对象"+arg0.getSource() + "被销毁了"); } public void requestInitialized(ServletRequestEvent arg0) { System.out.println("请求对象"+arg0.getSource() + "被创建了"); } }
在web.xml文件中配置
<listener> <listener-class>cn.csdn.web.listener.MyServletRequestListener</listener-class> </listener>
执行结果为:当访问idex.jsp时被创建随后被销毁了;
ServletRequestListener的实际用途:统计网站的请求数量,可以计算网站性能(实际应用不多)
二、监听三个域对象属性变化
创建类MyServletContextAttributeListener实现ServletContextAttributeListener
public class MyServletContextAttributeListener implements ServletContextAttributeListener { public void attributeAdded(ServletContextAttributeEvent arg0) { ServletContext context = arg0.getServletContext(); Object data = arg0.getValue(); System.out.println(context + "中加入了"+data); } public void attributeRemoved(ServletContextAttributeEvent arg0) { ServletContext context = arg0.getServletContext(); Object data = arg0.getValue(); System.out.println(context + "中删除了"+data); } public void attributeReplaced(ServletContextAttributeEvent arg0) { ServletContext context = arg0.getServletContext(); Object data = arg0.getValue(); System.out.println(context + "中更新了"+data); } }
在web.xml文件中配置
<listener> <listener-class>cn.csdn.web.listener.MyservletContextAttributeListener</listener-class> </listener>index.jsp中写到:
<% application.setAttribute("param","xxx"); application.setAttribute("param","yyy"); application.removeAttribute("param"); %>执行结果:
public class MyHttpSessionAndRequestAttributeListener implements HttpSessionAttributeListener, ServletRequestAttributeListener { public void attributeAdded(HttpSessionBindingEvent se) { System.out.println("session中增加了属性"); } public void attributeRemoved(HttpSessionBindingEvent se) { System.out.println("session中删除了属性"); } public void attributeReplaced(HttpSessionBindingEvent se) { System.out.println("session中属性被替换了"); } public void attributeAdded(ServletRequestAttributeEvent srae) { System.out.println("request中增加了属性"); } public void attributeRemoved(ServletRequestAttributeEvent srae) { System.out.println("request中删除了属性"); } public void attributeReplaced(ServletRequestAttributeEvent srae) { System.out.println("request中属性被替换了"); } }web配置:
<listener> <listener-class>cn.csdn.web.listener.MyHttpSessionAndRequestAttributeListener</listener-class> </listener>
<% session.setAttribute("param","aaa"); session.setAttribute("param","bbb"); session.removeAttribute("param"); request.setAttribute("param","aaa"); request.setAttribute("param","bbb"); request.removeAttribute("param"); %>
实现了HttpSessionBindingListener接口的 JavaBean 对象可以感知自己被绑定到 Session 中和从 Session 中删除的事件
创建user实现HttpSessionBindingListener
public class User implements HttpSessionBindingListener{ private String name; public void valueBound(HttpSessionBindingEvent event) { System.out.println("到session中去"); } public void valueUnbound(HttpSessionBindingEvent event) { System.out.println("从session中移除"); } public String getName() { return name; } public void setName(String name) { this.name = name; } }index.jsp中写:
<% session.setAttribute("user",new User()); session.removeAttribute("param"); %>
不用配置web文件
执行结果:一访问网页
到session中去
从session中移除
实际用途:
统计网站在线用户(和在线人数不同),当前网站在线用户(作业)
实现了HttpSessionActivationListener接口的 JavaBean 对象可以感知自己被活化和钝化的事件
创建person类实现HttpSessionActivationListener接口public class Person implements HttpSessionActivationListener, Serializable { public void sessionDidActivate(HttpSessionEvent arg0) { System.out.println("session从硬盘读入内存了"); } public void sessionWillPassivate(HttpSessionEvent arg0) { System.out.println("session被存到硬盘中去了"); } }index.jsp写入:
<% session.setAttribute("person",new Person()); %>
到服务器对应目录中查看钝化后的session文件
C:\apache-tomcat-7.0.22\work\Catalina\localhost\2011-11-24\class3g\AE40CDFE5F5018FAED45AACE025E4EC2.session
最后一个案例:显示登陆用户列表,并实现踢人功能:下载地址:http://download.csdn.net/detail/rhljiayou/3838511