1.创建监听器
在chapter08项目中创建一个cn.itcst.chapter08.listener包,在该包中编写一个MyListener类,这个类实现了ServletContextListener, HttpSessionListener, ServletRequestListener,这3个监听器接口,并实现这些接口中的所有方法。
创建listener类方法如下:
MyListener类中的代码如下:
package cn.itcast.chapter08.listener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.ServletRequestEvent; import javax.servlet.ServletRequestListener; import javax.servlet.annotation.WebListener; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; @WebListener public class MyListener implements ServletContextListener, HttpSessionListener, ServletRequestListener { public void sessionCreated(HttpSessionEvent arg0) { System.out.println("HttpSession对象被创建了"); } public void requestDestroyed(ServletRequestEvent arg0) { System.out.println("ServletRequest对象被销毁了"); } public void requestInitialized(ServletRequestEvent arg0) { System.out.println("ServletRequest对象被创建了"); } public void sessionDestroyed(HttpSessionEvent arg0) { System.out.println("HttpSession对象被销毁了"); } public void contextDestroyed(ServletContextEvent arg0) { System.out.println("ServletContext对象被销毁了"); } public void contextInitialized(ServletContextEvent arg0) { System.out.println("ServletContext对象被创建了"); } }
2.添加监听器类信息
在chapter08项目下的web.xml文件中,添加MyListener事件监听器的信息,代码如下:
cn.itcast.chapter08.listener.MyListener
3.启动项目
查看ServletContext对象创建信息,如图:
4.关闭项目
查看ServletContext对象销毁信息,如图:
5.创建测试页面
在chapter08项目的WebContent目录中编写一个简单的页面文件myjsp.jsp,如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>this is MyJsp.jsp 监听器正在监听
6.设置监听超时信息
在chapter08的web.xml文件中设置session的超时时间为2min,代码如下:
2
7.重启Tomcat服务器
在地址栏输入“http://localhost:2222/chapter08/MyListener”,访问myjsp.jsp页面,控制台窗口效果如图:
关闭myjsp.jsp页面或者保持页面不刷新,等待2min左右,HttpSession对象将会被销毁,如下:
2.监听域对象的属性变更1.创建测试页面
在chapter08项目的WebContext的根目录中,编写一个testattribute.jsp页面,以观察各个域对象属性事件监听器的作用,代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>Insert title here 这是一个测试对象属性信息监听器的页面 <% getServletContext().setAttribute("username", "itcast"); getServletContext().setAttribute("username", "itheima"); getServletContext().removeAttribute("username"); session.setAttribute("username", "itcast"); session.setAttribute("username", "itheima"); session.removeAttribute("username"); request.setAttribute("username", "itcast"); request.setAttribute("username", "itheima"); request.removeAttribute("username"); %>
2.创建监听器
在chapter08项目的cn.itcst.chapter08.listener包中,编写一个名称为MyAttributeListener的监听器类,它实现了ServletContextAttributeListener, HttpSessionAttributeListener, ServletRequestAttributeListener接口,实现这些接口中的所有方法,如下:
package cn.itcast.chapter08.listener; import javax.servlet.ServletContextAttributeEvent; import javax.servlet.ServletContextAttributeListener; import javax.servlet.ServletRequestAttributeEvent; import javax.servlet.ServletRequestAttributeListener; import javax.servlet.annotation.WebListener; import javax.servlet.http.HttpSessionAttributeListener; import javax.servlet.http.HttpSessionBindingEvent; public class MyAttributeListener implements ServletContextAttributeListener, HttpSessionAttributeListener, ServletRequestAttributeListener { public void attributeAdded(ServletContextAttributeEvent sae) { String name = sae.getName(); System.out.println("ServletContext添加属性:" + name + "=" + sae.getServletContext().getAttribute(name)); } public void attributeRemoved(ServletContextAttributeEvent sae) { String name = sae.getName(); System.out.println("ServletContext移除属性:" + name); } public void attributeReplaced(ServletContextAttributeEvent sae) { String name = sae.getName(); System.out.println("ServletContext替换属性:" + name + "=" + sae.getServletContext().getAttribute(name)); } public void attributeRemoved(ServletRequestAttributeEvent sra) { String name = sra.getName(); System.out.println("ServletRequest添加属性:" + name + "=" + sra.getServletRequest().getAttribute(name)); } public void attributeAdded(ServletRequestAttributeEvent sra) { String name = sra.getName(); System.out.println("ServletRequest移除属性:" + name); } public void attributeReplaced(ServletRequestAttributeEvent sra) { String name = sra.getName(); System.out.println("ServletRequest替换属性:" + name + "=" + sra.getServletRequest().getAttribute(name)); } public void attributeAdded(HttpSessionBindingEvent hbe) { String name = hbe.getName(); System.out.println("HttpSession添加属性:" + name + "=" + hbe.getSession().getAttribute(name)); } public void attributeRemoved(HttpSessionBindingEvent hbe) { String name = hbe.getName(); System.out.println("HttpSession移除属性:" + name); } public void attributeReplaced(HttpSessionBindingEvent hbe) { String name = hbe.getName(); System.out.println("HttpSession替换属性:" + name + "=" + hbe.getSession().getAttribute(name)); } }
3.添加监听信息
在web.xml文件中,添加MyAttributeListener事件监听器信息,如下:
cn.itcast.chapter08.listener.MyAttributeListener
4.启动项目,查看结果
在地址栏输入“http://localhost:2222/chapter08/testattribute.jsp”,访问testattribute.jsp页面,控制台显示信息如下:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)