Listener采用了观察者模式(24种模式之一),Listener是servlet的监听器,他可以监听客户端的请求、服务器端的操作等, 通过监听器,可以自动激发一些操作。比如:监听在线用户数量

当增加一个HttpSession时,就会激发sessinCreated(HttpSessionEvent sce)方法,这样就可以给在线人数+1了。

常见的监听器接口:

ServletContextAttributeListener 监听对servletContext属性的操作,比如删除、增加、修改属性等

ServletContextListener 监听ServletContext,当创建ServletContext时,激发contextInitialized(ServletContextEvent sce)方法,当销毁ServletContext时,激发ContextDestory(ServletContextEvent sce)方法、

实例:

首先配置web.xml

<!--servlet 监听器 start-->
<listener>
<listener-class>com.listener.MyServletContextListener</listener-class>
</listener>

<listener>
<listener-class>com.listener.MyServletContextAttributeListener</listener-class>
</listener>
<!-- servlet 监听器 end -->

web.xml

  <!--servlet 监听器  start-->
  <listener>
      <listener-class>com.listener.MyServletContextListener</listener-class>
  </listener>

  <listener>
      <listener-class>com.listener.MyServletContextAttributeListener</listener-class>
  </listener>
  <!-- servlet 监听器 end -->

2.ServletContextListener接口的调用

/**
* 在xml中配置监听器
* ServletContextListener 坚挺servletContext,当创建servletContext时,激发ContextInitialized(ServletContextEvent sce)方法
* 当销毁servletContext时,激发contextDestoryed(ServletContextEvent sce)方法
*/
package com.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class MyServletContextListener implements ServletContextListener
{

@Override
public void contextDestroyed(ServletContextEvent arg0)
{
System.out.println(arg0.toString());

}

@Override
public void contextInitialized(ServletContextEvent arg0)
{
System.out.println(arg0.toString());
}

}

MyServletContextListener

/**
 * 在xml中配置监听器
 * ServletContextListener 坚挺servletContext,当创建servletContext时,激发ContextInitialized(ServletContextEvent sce)方法
 * 当销毁servletContext时,激发contextDestoryed(ServletContextEvent sce)方法
 */
package com.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class MyServletContextListener implements ServletContextListener
{

    @Override
    public void contextDestroyed(ServletContextEvent arg0)
    {
        System.out.println(arg0.toString());

    }

    @Override
    public void contextInitialized(ServletContextEvent arg0)
    {
        System.out.println(arg0.toString());
    }

}

3.ServletContextAttributeListener接口的调用

package com.listener;

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;

public class MyServletContextAttributeListener implements ServletContextAttributeListener
{

@Override
public void attributeAdded(ServletContextAttributeEvent arg0)
{
System.out.println("attributeAdd ");
System.out.println(arg0.getName() + ":" + arg0.getValue());

}

@Override
public void attributeRemoved(ServletContextAttributeEvent arg0)
{
System.out.println("attributeRemoved");
System.out.println(arg0.getName() + ":" + arg0.getValue());//value值将显示之前的 值
}

@Override
public void attributeReplaced(ServletContextAttributeEvent arg0)
{
System.out.println("attributeRepaced");
System.out.println(arg0.getName() + ":" + arg0.getValue());//value值将显示之前的 值

}

}

MyServletContextAttributeListener

package com.listener;

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;

public class MyServletContextAttributeListener implements ServletContextAttributeListener
{

    @Override
    public void attributeAdded(ServletContextAttributeEvent arg0)
    {
        System.out.println("attributeAdd ");
        System.out.println(arg0.getName() + ":" + arg0.getValue());

    }

    @Override
    public void attributeRemoved(ServletContextAttributeEvent arg0)
    {
        System.out.println("attributeRemoved");
        System.out.println(arg0.getName() + ":" + arg0.getValue());//value值将显示之前的 值
    }

    @Override
    public void attributeReplaced(ServletContextAttributeEvent arg0)
    {
        System.out.println("attributeRepaced");
        System.out.println(arg0.getName() + ":" + arg0.getValue());//value值将显示之前的 值

    }

}

为了方便,没有用servlet举例,直接写的jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

<%

application.setAttribute("name1","value1");
application.setAttribute("name2","value2");
application.setAttribute("name1","value3");
application.setAttribute("name2","value4");
%>
<a href="listener2.jsp">next</a>
</body>
</html>

listener1.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

<%

    application.setAttribute("name1","value1");
    application.setAttribute("name2","value2");
    application.setAttribute("name1","value3");
    application.setAttribute("name2","value4");
%>
<a href="listener2.jsp">next</a>
</body>
</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
application.removeAttribute("name1");
%>
</body>
</html>

listener2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
    application.removeAttribute("name1");
%>
</body>
</html>

结束。listener在实际项目开发中,用到的不多。这里最好知道有这么回事就成。

珍惜现在,珍惜每一分,每一秒。 从不放弃,坚持。。。。。

servlet监听器Listener(理论+例子)的更多相关文章

  1. java之Servlet监听器Listener

    常用应用场景:单点登录.统计在线人数 一.简介 (一)概述 1.Listener 用于监听 java web程序中的事件,例如创建.修改.删除Session.request.context等,并触发响 ...

  2. Servlet 监听器Listener详解

    转自:http://blog.csdn.net/u012228718/article/details/41730799 一.简介 (一)概述 1.Listener 用于监听 Javaweb程序中的事件 ...

  3. Java进阶(十三)servlet监听器

    servlet监听器 Listener是Servlet的监听器,它可以监听客户端的请求.服务端的操作等.通过监听器,可以自动激发一些操作,比如监听在线的用户的数量.当 增加一个HttpSession时 ...

  4. Servlet监听器——实现在线登录人数统计小例子

    一.概念 servlet监听器的主要目的是给web应用增加事件处理机制,以便更好的监视和控制web应用的状态变化,从而在后台调用相应处理程序. 二.监听器的类型 1.根据监听对象的类型和范围,分为3类 ...

  5. [原创]java WEB学习笔记47:Servlet 监听器简介, ServletContext(Application 对象), HttpSession (Session 对象), HttpServletRequest (request 对象) 监听器,利用listener理解 三个对象的生命周期

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  6. Servlet的监听器Listener

    Servlet的监听器Listener,它是实现了javax.servlet.ServletContextListener 接口的服务器端程序,它也是 随web应用的启动而启动,只初始化一次,随web ...

  7. 监听器Listener

    监听器 6个事件类,均以event结尾 *某些操作,如启动/关闭容器,创建/销毁会话,都将触发一种事件发生,当发生了某种事件,容器将创建对应的事件类对象 8个监听接口,均以Listener结尾 监听器 ...

  8. JavaWeb—监听器Listener

    1.简介 Listener是Servlet的监听器,Servlet 监听器用于监听一些重要事件的发生,监听器对象在事情发生前.发生后可以做一些必要的处理. JavaWeb里面的listener是通过观 ...

  9. Spring Boot使用监听器Listener

    之前介绍了在Spring Boot中使用过滤器:https://www.cnblogs.com/zifeiy/p/9911056.html 接下来介绍使用监听器Listener. 下面是一个例子: p ...

随机推荐

  1. django复习笔记2:models

    关于models,主要想说的是django shell以及生成测试数据的脚本这两部分. 一个models中的类相当于数据库的一张表,先看一个设置了外键的models. from django.db i ...

  2. sublime自定义快键键不行,

    其实sublime自身就有格式化命令,就不再安装插件,位置在[Edit]->[Line]->[Reindent]但这个默认的命令没有快捷键,就重新定义了一下,想用习惯了的eclipse快捷 ...

  3. win7 远程桌面关机

    在任务管理器中, 打开运行窗口, 执行 shutdown -s 命令, 将在30秒后关闭win7, 如果需要更快, 加上 -t 10 参数 关于 shutdown 的命令行说明: C:\Users\R ...

  4. CSS3 3D骰子

    z zz zz zzzz zzzzz zzzzzz

  5. nfs 三个参数权限

    遇到nfs客户端不可写的情况.  有延迟啊啊啊..  等1min左右就可以写了. 挂载参数: cat /var/lib/nfs/etab -->server cat /proc/mounts   ...

  6. codevs http://www.codevs.cn/problem/?problemset_id=1 循环、递归、stl复习题

    12.10高一练习题 1.要求: 这周回顾复习的内容是循环.递归.stl. 不要因为题目简单就放弃不做,现在就是练习基础. 2.练习题: (1)循环   题目解析与代码见随笔分类  NOI题库 htt ...

  7. codevs1842 递归第一次

    难度等级:白银 1842 递归第一次 题目描述 Description 同学们在做题时常遇到这种函数 f(x)=5 (x>=0) f(x)=f(x+1)+f(x+2)+1 (x<0) 下面 ...

  8. SQL Server数据库代码指令简介

    这些是比较常用的命令操作,事先声明,这些命令是不区分大小写的,我按照我的课本来总结用法和知识点,无用的章节自动省略. 没有一点数据库知识基础的可以等我录制视频,不然可能看不懂,视频链接:http:// ...

  9. mac/linux中vim永久显示行号、开启语法高亮

    步骤1: cp /usr/share/vim/vimrc ~/.vimrc 先复制一份vim配置模板到个人目录下 注:redhat 改成 cp /etc/vimrc ~/.vimrc 步骤2: vi ...

  10. 数据库MySQL与Oracle的一些去O注意项

    一.oracle递归查询语句start with ...connect by prior ① 给你一张表,表里面有主键id,以及该项的父节点parent_id,查询出该表中所有的父子关系节点树? Or ...