JSP/Servlet 中的事件处理写过AWT或Swing程序的人一定对桌面程序的事件处理机制印象深刻:通过实现Listener接口的类可以在特定事件(Event)发生时,呼叫特定的方法来对事件进行响应。

其实我们在编写JSP/Servle程序时,也有类似的事件处理机制,所不同的是在JSP/Servlet中是在web.xml中注册Listener,由Container在特定事件发生时呼叫特定的实现Listener的类。

1. Servlet中的Listener和Event:

在JSP 2.0/Servlet 2.4中,共有八个Listener接口,六个Event类别。 
ServletContextListener接口 
[接口方法] contextInitialized()与 contextDestroyed() 
[接收事件] ServletContextEvent 
[触发场景] 在Container加载Web应用程序时(例如启动 Container之后),会呼叫contextInitialized(),而当容器移除Web应用程序时,会呼叫contextDestroyed ()方法。

ServletContextAttributeListener 
[接口方法] attributeAdded()、 attributeReplaced()、attributeRemoved() 
[接收事件] ServletContextAttributeEvent 
[触发场景] 若有对象加入为application(ServletContext)对象的属性,则会呼叫attributeAdded(),同理在置换属性与移除属性时,会分别呼叫attributeReplaced()、attributeRemoved()。

HttpSessionListener 
[接口方法] sessionCreated()与sessionDestroyed () 
[接收事件] HttpSessionEvent 
[触发场景] 在session (HttpSession)对象建立或被消灭时,会分别呼叫这两个方法。

HttpSessionAttributeListener 
[接口方法] attributeAdded()、 attributeReplaced()、attributeRemoved() 
[接收事件] HttpSessionBindingEvent 
[触发场景] 若有对象加入为session(HttpSession)对象的属性,则会呼叫attributeAdded(),同理在置换属性与移除属性时,会分别呼叫attributeReplaced()、 attributeRemoved()。

HttpSessionActivationListener 
[接口方法] sessionDidActivate()与 sessionWillPassivate() 
[接收事件] HttpSessionEvent 
[触发场景] Activate与Passivate是用于置换对象的动作,当session对象为了资源利用或负载平衡等原因而必须暂时储存至硬盘或其它储存器时(透过对象序列化),所作的动作称之为Passivate,而硬盘或储存器上的session对象重新加载JVM时所采的动作称之为Activate,所以容易理解的,sessionDidActivate()与 sessionWillPassivate()分别于Activeate后与将Passivate前呼叫。

ServletRequestListener 
[接口方法] requestInitialized()与 requestDestroyed() 
[接收事件] RequestEvent 
[触发场景] 在request(HttpServletRequest)对象建立或被消灭时,会分别呼叫这两个方法。

ServletRequestAttributeListener 
[接口方法] attributeAdded()、 attributeReplaced()、attributeRemoved() 
[接收事件] HttpSessionBindingEvent 
[触发场景] 若有对象加入为request(HttpServletRequest)对象的属性,则会呼叫attributeAdded(),同理在置换属性与移除属性时,会分别呼叫attributeReplaced()、 attributeRemoved()。

HttpSessionBindingListener 
[接口方法] valueBound()与valueUnbound() 
[接收事件] HttpSessionBindingEvent 
[触发场景] 实现HttpSessionBindingListener接口的类别,其实例如果被加入至session(HttpSession)对象的属性中,则会呼叫 valueBound(),如果被从session(HttpSession)对象的属性中移除,则会呼叫valueUnbound(),实现HttpSessionBindingListener接口的类别不需在web.xml中设定。

2. 如何注册Servlet中的事件 
实现上面这几个接口的类别,除了HttpSessionBindingListener外,必须在web.xml中向容器注册,容器才会在对应的事件发生时呼叫对应的类别.如:

<listener>
<listener-class>com.listener.MyServletContextListener</listener-class>
</listener>

Listener采用了观察者模式 ,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

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

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
<%@ 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>

j2ee servlet listener的更多相关文章

  1. Servlet, Listener 、 Filter.

    Java Web的三大组件:Servlet, Listener . Filter. 使用Listener监听器:八大监听器: 第一组:用于监听Servlet三个域对象的创建与销毁 1. Servlet ...

  2. j2ee Servlet、Filter、Listener

    首先,JSP/Servlet规范中定义了Servlet.Filter.Listener这三种角色,并没有定义Interceptor这个角色,Interceptor是某些MVC框架中的角色,比如Stru ...

  3. Servlet Listener

    需要继承ServletContextListener接口. 代码: package com.my; import java.io.*; import javax.servlet.*; import j ...

  4. Servlet - Listener、Filter、Decorator

    Servlet 标签 : Java与Web Listener-监听器 Listener为在Java Web中进行事件驱动编程提供了一整套事件类和监听器接口.Listener监听的事件源分为Servle ...

  5. servlet,listener,filter,interceptor的关系

    1.servlet:servlet是一种运行服务器端的java应用程序,具有独立于平台和协议的特性,并且可以动态的生成web页面,它工作在客户端请求与服务器响应的中间层.最早支持 Servlet 技术 ...

  6. Servlet Listener(监听器)

    监听器 Listener 是一个实现特定接口的 Java 程序,这个程序专门用于监听另一个 Java 对象的方法调用或属性改变,当被监听对象发生上述事件后,监听器某个方法将立即自动执行.监听器的相关概 ...

  7. J2EE:Servlet上传文件到服务器,并相应显示

    Servlet 可以与HTML一起使用来允许用户上传文件到服务器 编辑上传文件的页面upload.html 注意事项:上传方式使用POST不能使用GET(GET不能上传文件) 表单 enctype 属 ...

  8. springboot之filter/listener/servlet

    简介 SpringBoot可以简化开发流程,但是在其中如何使用传统的J2EE servlet/listener/filter呢 @Bean配置 在Configuration类中加入filter和ser ...

  9. web.xml中的主要元素说明(listener, filter, servlet)

    web.xml中加载的顺序为:context-param ---> listener ---> filter ---> servlet. listener:主要针对的是对象的操作,如 ...

随机推荐

  1. wdate-year-month-week-gategory-amount-coin

    ---2016-12-02 19:46:39 the whole table DISTINCT field SUM(field) COUNT(field) --- 888983 rows OK SEL ...

  2. 1 2 3 n

    n(n+1)/2 连续自然数 1,2,3.....,n 队列 从中任意取出1至n个相加,可以表示的连续自然数队列中最大的自然数是多少 受"高斯求和--蛇头蛇尾脑图--长方形对角线脑图--苯环 ...

  3. Delphi Application.MessageBox详解

    引数:1. Text:要显示的讯息2. Caption:讯息视窗的标题列文字3. Flags:讯息旗标     3.1. 可指定讯息视窗上的图示     3.2. 可指定讯息视窗出现的按钮     3 ...

  4. Cloudera CDH 、Impala本地通过Parcel安装配置详解及什么是Parcel

    本文引用自:Cloudera CDH .Impala本地通过Parcel安装配置详解及什么是Parcelhttp://www.aboutyun.com/forum.php?mod=viewthread ...

  5. sp.net2.0中的新增控件BulletedList的一些高级用法

    asp.net2.0新增了一个BulletedList控件,通过它可以以列表形式显示数据,而不必再用Repeater,Datalist等实现相同的效果.今天做程序的时候正好用到了这个控件,就把它的一些 ...

  6. java简单优化和编写规范,自己总结的。

    1.永远不要比较两个浮点数是否相等.它是不安全的.详情google. 2.尽量使用StringBuffer代替String. 3.final类会提高很多效率. 4.try-catch 不应该用来控制程 ...

  7. java编程技巧

    欢迎提出建议指出错误互相交流. 1.统计对象数量,比如统计一共发射了多少颗子弹. public class Bullet { public static int count = 0; public B ...

  8. ArcGIS API for JavaScript开发环境搭建及第一个实例demo

    原文:ArcGIS API for JavaScript开发环境搭建及第一个实例demo ESRI公司截止到目前已经发布了最新的ArcGIS Server for JavaScript API v3. ...

  9. Anacodna之conda与 virtualenv对比使用教程,创建虚拟环境

    conda创建虚拟环境 1.查看包 conda list查看安装了哪些包 conda env list查看有哪些虚拟环境 conda -V查看conda的版本 2.创建虚拟环境,命名为myflaska ...

  10. Android笔记:Select at least one project解决办法

    导入项目的时候发现无法导入,最上方提示“Select at least one projec” 百度了一下,原来是和上一个项目名称相同了,直接把重名项目重命名后再导入即可.