后台(23)——Listener
探索Android软键盘的疑难杂症 深入探讨Android异步精髓Handler 详解Android主流框架不可或缺的基石 站在源码的肩膀上全解Scroller工作机制 Android多分辨率适配框架(1)— 核心基础 Android多分辨率适配框架(2)— 原理剖析 Android多分辨率适配框架(3)— 使用指南 自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onLayout源码详尽分析 自定义View系列教程04–Draw源码分析及其实践 自定义View系列教程05–示例分析 自定义View系列教程06–详解View的Touch事件处理 自定义View系列教程07–详解ViewGroup分发Touch事件 自定义View系列教程08–滑动冲突的产生及其处理 版权声明 本文原创作者:谷哥的小弟 作者博客地址:http://blog.csdn.net/lfdfhl Listener简介 JavaWeb开发中我们有时需要监听域对象HttpServletRequest、HttpSession、ServletContext的创建与销毁及其属性变化;除此以外,有时还需要监听session对JavaBean的绑定和解绑。这些操作我们都可以用Listener来实现。 监听域对象的创建与销毁 在此涉及到三个监听器ServletContextListener、HttpSessionListener以及ServletRequestListener。 监听器的使用步骤 1、创建类,实现指定的监听器接口 2、实现接口中的方法 3、在web.xml文件中注册监听器 鉴于这三个监听器的使用非常类似,故在此以ServletContextListener为例说明其使用方式。 首先创建一个类实现ServletContextListener接口且实现其中的方法 /** * 本文作者:谷哥的小弟 * 博客地址:http://blog.csdn.net/lfdfhl */ package cn.com; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class TestServletContextListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("ServletContext对象被创建"); } @Override public void contextDestroyed(ServletContextEvent sce) { System.out.println("ServletContext对象被销毁"); } } 然后我们在web.xml中注册该监听器即可 <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <listener> <listener-class>cn.com.TestServletContextListener</listener-class> </listener> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> 监听域对象的属性变化 在此涉及到三个监听器ServletContextAttributeListener、HttpSessionAttributeListener以及ServletRequestAttributeListener 监听器的使用步骤 1、创建类,实现指定的监听器接口 2、实现接口中的方法 3、在web.xml文件中注册监听器 鉴于这三个监听器的使用非常类似,故在此以ServletRequestAttributeListener为例说明其使用方式。 首先创建一个类实现ServletRequestAttributeListener接口且实现其中的方法 /** * 本文作者:谷哥的小弟 * 博客地址:http://blog.csdn.net/lfdfhl */ package cn.com; import javax.servlet.ServletRequestAttributeEvent; import javax.servlet.ServletRequestAttributeListener; public class TestServletRequestAttributeListener implements ServletRequestAttributeListener { public void attributeAdded(ServletRequestAttributeEvent srae) { System.out.println("ServletRequest添加属性"); } public void attributeRemoved(ServletRequestAttributeEvent srae) { System.out.println("ServletRequest移除属性"); } public void attributeReplaced(ServletRequestAttributeEvent event) { System.out.println("ServletRequest替换属性"); System.out.println(event.getName()+" "+event.getValue()); } } 然后我们在web.xml中注册该监听器即可 <listener> <listener-class>cn.com.TestServletRequestAttributeListener</listener-class> </listener> 嗯哼,我们来在index.jsp中测试该监听器 <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="UTF-8"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>测试</title> <style type="text/css"> p { font-size: 30px; font-family: 宋体; color: red; background-color: pink; } </style> </head> <body> <p>原创作者:谷哥的小弟</p> <p>博客地址:http://blog.csdn.net/lfdfhl</p> <% request.setAttribute("name", "小泽玛利亚"); request.setAttribute("name", "大泽玛利亚"); request.removeAttribute("name"); %> </body> </html> 监听session对JavaBean的绑定和解绑 可利用HttpSessionBindingListener监听session对JavaBean的绑定和解绑 使用步骤 1、创建JavaBean实现Serializable和HttpSessionBindingListener 2、实现接口中的方法 请看如下示例 我们先创建一个JavaBean /** * 本文作者:谷哥的小弟 * 博客地址:http://blog.csdn.net/lfdfhl */ package cn.com; import java.io.Serializable; import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionBindingListener; public class Student implements Serializable,HttpSessionBindingListener { private int studentID; private String studentName; public int getStudentID() { return studentID; } public void setStudentID(int studentID) { this.studentID = studentID; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } @Override public String toString() { return "Student [studentID=" + studentID + ", studentName=" + studentName + "]"; } @Override public void valueBound(HttpSessionBindingEvent event) { System.out.println("Student对象被绑定"); } @Override public void valueUnbound(HttpSessionBindingEvent event) { System.out.println("Student对象被解绑"); } } 嗯哼,我们再在index.jsp中测试该监听器 <%@page import="cn.com.Student"%> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="UTF-8"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>测试</title> <style type="text/css"> p { font-size: 30px; font-family: 宋体; color: red; background-color: pink; } </style> </head> <body> <p>原创作者:谷哥的小弟</p> <p>博客地址:http://blog.csdn.net/lfdfhl</p> <% session.setAttribute("student", new Student()); %> </body> </html>