第11章 Filter与 Listener


教学要求

(1) 熟练掌握:Filter与 Listener使用
(2)
(3)

延伸阅读


11.1 Filter


11.1.1 什么是过滤器

过滤器的处理方式
图1 过滤器的处理方式
过滤器链
图2 过滤器链(chain)

11.1.2 过滤器初识和过滤器的生命周期

【例1】 Filter生命周期

Filter生命周期:在请求访问ch11_du/1/*资源时,运行此过滤器。

(1) 创建的Filer类:在scr创建cn.dzj.ch11.filter包,创建LifeCycleFilter_000.java。右击[cn.dzj.ch11.filter]包 --> 新建 -- Filter -- class name:LifeCycleFilter_000 -- 下一步 -- Filer mapping -- LifeCycleFilter_000 -- Edit -- Pattern:/ch11_du/1/* -- 完成

创建Filter操作示意
图3 创建Filter操作示意

(2) 在WebContent/ch11_du/创建文件夹1,在1文件夹创建下列test.txt

(3) 在WebContent/ch11_du/1/创建test.html

(4) 在WebContent/ch11_du/1/创建1个小图片test.jpg

创建Filter操作示意
图4 小图片test.jpg

(5) 在WebContent/ch11_du/index01_000.html

Filter生命周期 index01_000.html

11.1.3 过滤器核心对象

过滤器配置

11.1.4 filter应用举例

【例2】 网站首页index.jsp访问计数过滤器

(1) 在src下cn.dzj.ch11.filter包,创建CountFilter_000.java。

(2) 在WebContent/ch11_du创建文件index02_000.jsp

网站首页访问计数

【例3】 图片资源防盗过滤器(保护/images/下资源)

(1) 在src下cn.dzj.ch11.filter包,创建ImageRedirectFilter_000.java。

创建Filter操作示意
图5 防盗图片/error.gif

(2) 在WebContent/ch11_du/创建文件index03_000.html

(3) 在WebContent/ch11_du/index03_000_2.html

(4) 创建WebContent/images文件夹, 并创建126.jpg,127.jpg,128.jpg,129.jpg,test.txt 5个文件

图片资源防盗过滤器之一

  

图片资源防盗过滤器之二

复制[index03_000_2.html]到桌面并用浏览器打开测试之。

【例4】 Login过滤器(只有登录的用户才能访问/ch11_du/4/目录及其子目录的资源)

(1) 在src下cn.dzj.ch11.filter包,创建LoginFilter_000.java。

(2) 在src下cn.dzj.ch11.servlet包,创建LoginServlet_000.java。

(3) 在WebContent/ch11_du/2创建文件[登录及其未登录资源访问测试界面]login.jsp

(4) 在WebContent/ch11_du/2创建文件[登录成功后的主界面及其登录成功的资源访问测试界面]index.jsp

(5) 在WebContent/ch11_du/2创建文件[登录成功后的登出界面]logOut.jsp

(6) 在WebContent/ch11_du/2和其子目录下创建下列个建3个文件:test.txt[莫愁前路无知己,天下谁人不识君。]、test.htm[劝君更尽一杯酒,西出阳关无故人。]、和1个小图片test.jpg,单击下列链接测试和停止tomcat,测试时观察[控制台]输出,测试完毕后屏蔽LoginFilter.java第20-22行注入,停止此过滤器再次运行。

Login过滤器

【例5】敏感文字过滤(过滤:糟糕,混蛋,他妈的,他奶奶的)

(1) 在src下cn.dzj.ch11.filter包,创建WordFilter_000.java。

(2) 在src下创建cn.dzj.ch11.servlet包,创建MessageServlet_000.java。

(3) 在WebContent/ch11_du/5 创建文件result_000.jsp

(4) 在WebContent/ch11_du/5创建文件index_000.jsp

敏感文字过滤(过滤:糟糕,混蛋,他妈的,他奶奶的) index_000.jsp

11.2 监听器

11.2.1 监听器简介

Servlet3.1中web对象、监听器接口与其事件的对应关系
Web对象 Listener接口 Event(事件) 方法
application(应用)
或(ServletContext上下文)
ServletContextListener(Servlet上下文监听器) ServletContextEvent(生命周期事件) contextInitialized(ServletContextEvent arg0)--初始化
contextDestroyed(ServletContextEvent arg0)--销毁
ServletContextAttributeListener(Servlet上下文属性监听器) ServletContextAttributeEvent(属性改变事件) attributeAdded(ServletContextAttributeEvent arg0)--增加后
attributeRemoved(ServletContextAttributeEvent event)--移除后
attributeReplaced(ServletContextAttributeEvent event)--替换
session(会话)
或(HttpSession Http会话)
HttpSessionListener(Http会话监听器) HttpSessionEvent(生命周期事件) sessionCreated(HttpSessionEvent se)--创建后
sessionDestroyed(HttpSessionEvent se)--销毁后
HttpSessionAttributeListener(Http会话属性监听器) HttpSessionBindingEvent(属性改变事件) attributeAdded(HttpSessionBindingEvent event)--增加后
attributeRemoved(HttpSessionBindingEvent event)--移除后
attributeReplaced(HttpSessionBindingEvent event)--替换
HttpSessionActivationListener(http会话激活监听器) HttpSessionEvent(会话迁移事件) sessionDidActivate(HttpSessionEvent se)--会话激活
sessionWillPassivate(HttpSessionEvent se)--会话钝化
HttpSessionBindingListener(http会话激活监听器) HttpSessionBindingEvent(对象绑定事件) valueBound(HttpSessionBindingEvent event)--绑定
valueUnbound(HttpSessionBindingEvent event)--解绑
HttpSessionIdListener(http会话ID改变监听器) HttpSessionEvent(会话迁移事件) sessionDidActivate(HttpSessionEvent se)--会话激活
sessionWillPassivate(HttpSessionEvent se)--会话钝化
request(请求)
或(ServletRequest Servlet请求)
ServletRequestListener(Servlet请求监听器) ServletRequestEvent(生命周期事件) requestInitialized(ServletRequestEvent arg0)--请求初始化
requestDestroyed(ServletRequestEvent arg0)--请求销毁
ServletRequestAttributeListener(Servlet请求属性监听器) ServletRequestAttributeEvent(请求属性改变事件) attributeAdded(ServletRequestAttributeEvent arg0)--增加后
attributeRemoved(ServletRequestAttributeEvent arg0)--移除后
attributeReplaced(ServletRequestAttributeEvent arg0)--替换
AsyncListener(异步请求监听器) AsyncEvent(异步请求事件) onStartAsync(AsyncEvent arg0)--异步开始
onComplete(AsyncEvent arg0)--异步完成
onTimeout(AsyncEvent arg0)--异步超时
onError(AsyncEvent arg0)--异步出错
ServletContextListener ServletContextEvent ServletContextAttributeListener ServletContextAttributeEvent HttpSessionListener HttpSessionEvent HttpSessionAttributeListener HttpSessionBindingEvent HttpSessionIdListener HttpSessionEvent HttpSessionActivationListener HttpSessionEvent HttpSessionBindingListener HttpSessionBindingEvent ServletRequestListener ServletRequestEvent ServletRequestAttributeListener ServletRequestAttributeEvent AsyncListener AsyncEvent
过滤器链
图6 监听器概述

11.2.2 servlet上下文监听

1. javax.servlet.ServletContextListener接口:监听ServletContext对象的初始化、销毁事件

2. javax.servlet.ServletAttributeListener接口:监听ServletContext对象属性添加、修改和删除事件

过滤器链
图7 创建监听器步骤图示

【例6】 ServletContextListener监听器

在src下创建cn.dzj.ch11.listener包,创建实现了ServletContextListener接口的MyServletContentListener_000.java监听器。

【例7】 ServletContextListener监听器

(1) 在src下cn.dzj.ch11.listener包,创建实现了ServletConnextAtrributeListener接口的MyServletContextAttributeListener_000.java监听器。

(2) 创建[测试]页面:WebContent\ch11_du\index07_000.jsp,启停web服务器在控制台观看器输出提示。完毕后屏蔽@WebListener注解停此监听器。

application属性变化监听器测试 index07_000.jsp

11.2.3 HttpSession监听

1. 在Servlet API中定义了javax.servlet.http.HttpSession接口,Servlet容器必须实现这一个接口。当一个Session开始时,Servlet容器将创建一个HttpSession对象,在HttpSession对象中可以存放客户状态的信息。Servlet容器为HttpSession对象分配一个唯一的标识符,叫做Session ID,Servlet容器把Session ID作为Cookie保存在客户的浏览器中,每次客户发出HTTP请求时,Servlet容器可以从HttpRequest对象中读取Session ID,然后根据Session ID找到相应的HttpSession对象,从而获得客户的状态信息。HttpSession接口有如下方法:

2. javax.servlet.http.HttpSessionListener接口: 监听Session对象的初始化、销毁事件

3. javax.servlet.http.HttpSessionAttributeListener接口:监听HttpSession对象属性添加、修改和移除事件

【例8】 HttpSession监听

(1) 在src下cn/dzj/ch11/listener包,创建实现了HttpSessionListener,HttpSessionAttributeListener接口的MyHttpSessionListener_000.java监听器。

(2) 创建[设置session属性]jsp页面:WebContent\ch11_du\session_setAttribute_000.jsp

(3) 创建[移除session属性]jsp页面:WebContent\ch11_du\session_removeAttribute_000.jsp

(4) 创建[设置session有效时间]jsp页面:WebContent\ch11_du\session_setMaxInactiveInterval_000.jsp

(5) 创建[销毁session]jsp页面:WebContent\ch11_du\session_invalidate_000.jsp

(6) 创建[设置会话属性]jsp页面:WebContent\ch11_du\session_000.jsp

(7) 创建[显示答案(获取会话属性)]jsp页面:WebContent\ch11_du\result08_000.jsp

(8) 创建[用户注册]页面:WebContent\ch11_du\index08_000.jsp,启停web服务器在控制台观看器输出提示。完毕后屏蔽@WebListener注解停此监听器。

session监听器测试 index08_000.jsp

【例9】 监听器统计在线人数

Jsp/servlet标准不要求一个web容器支持分布式应用,但是他一定要支持HttpSessionActivationListener接口,以使代码可以支持分布式环境。
一般免费的web容器都不支持分布式,weblogic websphere是支持的。为了负载均衡可以迁移一个session到其他的jvm.session的passivation(钝化---非活化)。
passivation是指非活动的session被写入持久设备(比如硬盘)。
activate自然就是相反的过程。
在分布式环境中切换的属性必须实现serializable接口。
一般情况下他和HttpSessionBindingListener一起使用。

3. javax.servlet.http.HttpSessionActivationListener接口:

4. javax.servlet.http.HttpSessionBindingListener接口:

(1) 在src下cn/dzj/ch11/listener包,创建UserInfoList.java

(2) 在src下cn/dzj/ch11/listener包,创建实现了HttpSessionBindingListener接口的UserInfoTrace.java监听器。

(3) 创建[用户登出]jsp页面:WebContent\ch11_du\loginOut09_000.jsp

(4) 创建[用户列表]页面:WebContent\ch11_du\showUser09_000.jsp

(5) 创建[使用监听查看在线用户(用户登录)]页面:WebContent\ch11_du\index09_000.jsp

监听器统计在线人数 index09_000.jsp

11.2.4 servlet请求监听

【例10】 利用监听使服务器端机器免登录

(1) 在src下cn/dzj/ch11/listener包,创建实现了ServletRequestListener, ServletRequestAttributeListener接口的Listenerlocalhost_000.java监听器。

(2) 创建[用户登录]页面:WebContent\ch11_du\index10_000.jsp

(3) 创建[系统主页面]页面:WebContent\ch11_du\show10_000.htm

服务器端免登录 index10_000.jsp

11.3 servlet 3.0新特性

11.3.1 新增注入

11.4 习题


1.填空题(概念、知识点----------讲义中醒目的关键字)

(1)

(2)

(3)

(4)

2.操作题

(1)

(2)

(3)

(4)

3.编程题

jsp+javabean+servlet+filter+Cookie+jdbc的用户登录应用

【步骤1】 创建【用户】JavaBean、数据库表和数据库连接公用模块

(1) 在src下创建cn.dzj.ch11.bean包,创建JavaBean类Admin00.java。

(2) 在MySql中,创建或打开app_dujw数据库,并在此库中创建admin00数据表。

(3) 在src下创建cn.dzj.ch11.util包,创建数据库连接公用类DBConn.java。

【步骤2】 创建dao及其impl

(1) 在src下创建cn.dzj.ch11.dao包,创建dao数据访问接口BaseDao.java。

(2) 在src\cn.dzj.ch11.dao包,创建dao数据访问接口AdminDao00.java。

(3) 在src下创建cn.dzj.ch11.dao.impl包,创建AdminDao00接口的实现类AdminDaoImpl00.java。

【步骤3】 创建service及其impl

(1) 在src下创建cn.dzj.ch11.service包,并在包下创建BaseService.java

(2) 在src下cn.dzj.ch11.service包,创建AdminService00.java

(3) 在src下创建cn.dzj.ch11.service.impl包,创建AdminService00接口的实现类AdminServiceImpl00.java

【步骤4】 实创建[登录界面]、servlet类、filter类

(1) 创建[登录界面]页面:WebContent\jw_du\ch11_du\4\login_000.jsp

(2) 创建[用户登录]的servlet类:cn.dzj.ch11.servlet\AdminServlet00.java

(3) 创建[登录成功界面]:WebContent\jw_du\ch11_du\14\sc.jsp

(4) 创建Filter类:cn.dzj.ch11.filter\AutoLoginFilter_000.java

自动登录管理



本章参考文献



返回目录