一直以来,对监听器只是熟悉,一直在用,但是不知道具体的原理,最近看资料想写一下关于监听器的博客,以后时候可以用到!
一,什么是监听器:
监听器就是一个实现了特定接口的Java类,这个Java类用于监听另一个Java类的方法调用或者属性的改变。当被监听对象发生上述事件后,监听器某个方法将会立即被执行。
二,监听器的用途:
用来监听其他对象的变化的。主要应用在图形化界面开发上。
- Java中GUI, 2. Android
三,监听器解释的例子:
- 事件源:指的是被监听对象(汽车)
- 监听器:指的是监听的对象(报警器)
- 事件源和监听器绑定:在汽车上安装报警器
- 事件:指的是事件源对象的改变(踹了汽车一脚)----主要功能获得事件源对象。
Example:测试GUI的例子:(关闭按钮,触发一个事件)
package listener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
public class BaseListener {
public static void main(String[] args){
JFrame jFrame=new JFrame();
jFrame.setBounds(0, 0, 400, 300);
jFrame.setName("小窗口");
jFrame.setVisible(true);
jFrame.addWindowListener(new MyClose());
}
}
class MyClose implements WindowListener{
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
JFrame jFrame=(JFrame)e.getSource();
System.out.println(jFrame.getName()+"已经关闭了!");
System.exit(0);
}
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
}
JavaWeb中的监听器
- 一类:监听三个域对象的创建和销毁的监听器(三个)
- 二类:监听三个域对象的属性变更(属性添加、移除、替换)的监听器(三个)
- 三类:监听HttpSession中JavaBean的状态改变(钝化、活化、绑定、解除绑定)的监听(两个)
事件源:三大域!
- ServletContext
- 生命周期监听:ServletContextListener,它有两个方法,一个在出生时调用,一个在死亡时调用;
- void contextInitialized(ServletContextEvent sce):创建SErvletcontext时
- void contextDestroyed(ServletContextEvent sce):销毁Servletcontext时
- 属性监听:ServletContextAttributeListener,它有三个方法,一个在添加属性时调用,一个在替换属性时调用,最后一个是在移除属性时调用。
- void attributeAdded(ServletContextAttributeEvent event):添加属性时;
- void attributeReplaced(ServletContextAttributeEvent event):替换属性时;
- void attributeRemoved(ServletContextAttributeEvent event):移除属性时;
ServletContextListener企业用途:(加载框架的配置文件)
- Spring框架提供了一个核心监听器ContextLoaderListener。(spring里面的核心)
- 定时任务调度 (如session优化中,session的钝化,活化)
- HttpSession
- HttpSession创建和销毁
- 创建:
- 服务器端第一次调用getSession()方法。
- 销毁:
- 非正常关闭服务器(正常关闭服务器session会被序列化)。
- Session过期(默认过期时间30分钟)。
- 手动调用session.invalidate()方法。
访问HTML是否创建Session :不会
访问JSP是否创建Session :会
访问Servlet是否创建Session :不会(默认没有调用getSession方法)
- 生命周期监听:HttpSessionListener,它有两个方法,一个在出生时调用,一个在死亡时调用;
- void sessionCreated(HttpSessionEvent se):创建session时
- void sessionDestroyed(HttpSessionEvent se):销毁session时
- 属性监听:HttpSessioniAttributeListener,它有三个方法,一个在添加属性时调用,一个在替换属性时调用,最后一个是在移除属性时调用。
- void attributeAdded(HttpSessionBindingEvent event):添加属性时;
- void attributeReplaced(HttpSessionBindingEvent event):替换属性时
- void attributeRemoved(HttpSessionBindingEvent event):移除属性时
MyHttpSessionListener.java测试httpSession的例子(监听器配置文件省略)
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
/**
* 监听HttpSession的创建和销毁的监听器
* @author jt
*
*/
public class MyHttpSessionListener implements HttpSessionListener{
@Override
// 监听HttpSession的创建
public void sessionCreated(HttpSessionEvent se) {
System.out.println("HttpSession对象被创建了...");
}
@Override
// 监听HttpSession的销毁
public void sessionDestroyed(HttpSessionEvent se) {
System.out.println("HttpSession对象被销毁了...");
}
}
- ServletRequest
ServletRequest对象的创建和销毁
创建:从客户端向服务器发送一次请求,服务器就会创建request对象。
销毁:服务器对这次请求作出了响应之后,request对象就销毁了。
- 生命周期监听:ServletRequestListener,它有两个方法,一个在出生时调用,一个在死亡时调用;
- void requestInitialized(ServletRequestEvent sre):创建request时
- void requestDestroyed(ServletRequestEvent sre):销毁request时
- 属性监听:ServletRequestAttributeListener,它有三个方法,一个在添加属性时调用,一个在替换属性时调用,最后一个是在移除属性时调用。
- void attributeAdded(ServletRequestAttributeEvent srae):添加属性时
- void attributeReplaced(ServletRequestAttributeEvent srae):替换属性时
- void attributeRemoved(ServletRequestAttributeEvent srae):移除属性时
MyServletRequestListener.java测试java的servletRequestListener监听器
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
/**
* 监听ServletRequest对象的创建和销毁的监听器
* @author jt
*
*/
public class MyServletRequestListener implements ServletRequestListener{
@Override
public void requestInitialized(ServletRequestEvent sre) {
System.out.println("ServletRequest对象被创建了...");
}
@Override
public void requestDestroyed(ServletRequestEvent sre) {
System.out.println("ServletRequest对象被销毁了...");
}
}
- javaWeb中完成编写监听器:
- 写一个监听器类:要求必须去实现某个监听器接口;
- 注册,是在web.xml中配置来完成注册!
- 事件对象:
- ServletContextEvent:ServletContext getServletContext()
- HttpSessionEvent:HttpSession getSession()
- ServletRequest:
- ServletContext getServletContext();
- ServletReques getServletRequest();
- ServletContextAttributeEvent:
- ServletContext getServletContext();
- String getName():获取属性名
- Object getValue():获取属性值
- HttpSessionBindingEvent:略
- ServletRequestAttributeEvent :略
访问HTML页面是否创建请求对象 :会
访问JSP页面是否创建请求对象 :会
访问Servlet是否创建请求对象 :会
感知监听(都与HttpSession相关)
- 它用来添加到JavaBean上,而不是添加到三大域上!
- 这两个监听器都不需要在web.xml中注册!
HttpSessionBindingListener:添加到javabean上,javabean就知道自己是否添加到session中了。
钝化,活化的例子:
import java.io.Serializable;
import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionEvent;
/**
* 监听HttpSession中的对象的钝化和活化的监听器
* @author jt
*
*/
public class Bean2 implements HttpSessionActivationListener ,Serializable{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void sessionWillPassivate(HttpSessionEvent se) {
System.out.println("Bean2被session钝化了...");
}
@Override
public void sessionDidActivate(HttpSessionEvent se) {
System.out.println("Bean2被session活化了...");
}
}
JavaWeb监听器概述
在JavaWeb被监听的事件源为:ServletContext、HttpSession、ServletRequest,即三大域对象。
- 监听域对象“创建”与“销毁”的监听器;
- 监听域对象“操作域属性”的监听器;
- 监听HttpSession的监听器。
创建与销毁监听器一共有三个:
ServletContextListener:Tomcat启动和关闭时调用下面两个方法
- public void contextInitialized(ServletContextEvent evt):ServletContext对象被创建后调用;
- public void contextDestroyed(ServletContextEvent evt):ServletContext对象被销毁前调用;
HttpSessionListener:开始会话和结束会话时调用下面两个方法
- public void sessionCreated(HttpSessionEvent evt):HttpSession对象被创建后调用;
- public void sessionDestroyed(HttpSessionEvent evt):HttpSession对象被销毁前调用;
ServletRequestListener:开始请求和结束请求时调用下面两个方法
- public void requestInitiallized(ServletRequestEvent evt):ServletRequest对象被创建后调用;
- public void requestDestroyed(ServletRequestEvent evt):ServletRequest对象被销毁前调用。
事件对象
- ServletContextEvent:ServletContext getServletContext();
- HttpSeessionEvent:HttpSession getSession();
- ServletRequestEvent:
- ServletRequest getServletRequest()
- ServletContext getServletContext()
1.测试统计在线人数的例子:
1)OnLineCountHttpSessionListener.java(监听session创建的例子)
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class OnLineCountHttpSessionListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent se) {
// 在线了
HttpSession session = se.getSession();
System.out.println(session.getId()+"上线了...");
// 获得ServletContext中的值
Integer count = (Integer) session.getServletContext().getAttribute("count");
count++;
session.getServletContext().setAttribute("count", count);
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
// 离线了
HttpSession session = se.getSession();
System.out.println(session.getId()+"离线了...");
// 获得ServletContext中的值
Integer count = (Integer) session.getServletContext().getAttribute("count");
count--;
session.getServletContext().setAttribute("count", count);
}
}
2)OnLineCountServletContextListener.java (相同创建时,对servlet里面的值进行初始化)
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class OnLineCountServletContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
// 在服务器启动的时候初始化一个值为0
// 还需要将这个值存入到ServletContext中。
sce.getServletContext().setAttribute("count", 0);
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
}
3.当前访问的jsp页面home.jsp(测试的jsp页面)
<body>
<h1>当前访问人数:${ count }</h1>
</body>