网络

教育改变生活

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 585|回复: 0
打印 上一主题 下一主题

【JAVA WEB应用开发】8-1监听器

[复制链接]

354

主题

355

帖子

1464

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1464
跳转到指定楼层
楼主
发表于 2023-4-14 09:57:04 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
第八 监听器-1
8.1监听器Listener的工作原理及应用场景
1.什么是Servlet监听器?
Servlet监听器类似于JavaGUI组件的事件监听器,主要监听由于Web应用中的状态改变而引起的Servlet容器产生的相应事件,并进行相应的事件处理。
Servlet监听器是指实现了监听器接口的类,一般的监听器类需要在web.xml中配置才能产生作用。当Web应用的某个事件发生时(如:当Web服务启动或停止、session创建或销毁等等),相应的监听器将会被Servlet容器自动调用。
2应用场景
·统计当前在线人数
·页面访问量统计
·应用启动时完成信息初始化工作
·与spring结合
3、监听器分类
一共包含八个Listener接口可以将其分为三类,分别如下:
·第一类与ServletContext有关的Listener接口。
包括两个Listener接口分别是ServletContextListenerServletContextAttributeListener
·第二类与HttpSession有关的Listener接口。
包括四个Listener接口分别是HttpSessionListenerHttpSessionAttributeListener
HttpSessionBindingListenerHttpSessionActivationListener
·第三类与ServletRequest有关的Listener接口。
包括两个Listener接口分别是ServletRequestListenerServletRequestAttributeListener
8.2.监听器类型及相关API
·ServletContext监听器
1ServletContextListener接口
用来实现ServletContext的启动和销毁监听。该接口中包含两个方法
·contextInitialized()方法用来监听ServletContext的启动和初始化
·contextDestroyed()方法用来监听ServletContext的销毁。
在这两个方法中还包含一个参数sce其类型为ServletContextEvent。通过对象的getServletContext()方法可以获得ServletContext对象。
2ServletContextAttributeListener接口
    用来实现application范围属性变化的监听。该接口中包含三个方法
    ·attributeAdded()方法用来监听application范围属性的添加·attributeReplaced()方法用来监听application范围属性的替换
    ·attributeRemoved()方法用来监听application范围属性的移除。
在这两个方法中还包含一个参数scab其类型为ServletContextAttributeEventgetName()方法可以获得属性的名称,getValue()方法可以获得属性的值。
·session监听器
HttpSessionListener包含四个Listener接口,分别是:
·HttpSessionListener
·HttpSessionAttributeListener
·HttpSessionBindingListener
·HttpSessionActivationListener
其中HttpSessionListener接口用来实现sesion的初始化和销毁监听
HttpSessionAttributeListener接口用来实现session范围属性变化的监听。
·ServletReqeust监听器
8.3监听器管理共享数据库连接
ServletContextListener 接口,能够监听 ServletContext 对象的生命周期,实际上就是监听 Web 应用的生命周期。当Servlet 容器启动或终止Web 应用时,会触发ServletContextEvent 事件,该事件由ServletContextListener 来处理。
在服务启动时,触发contextInitialized事件,创建数据库连接Connection,并将其赋值给一个属性名,其它的 Servlet 就可以通过 getAttribute 进行属性值的访问,获取数据库连接,从而实现数据库连接的共享。
1、监听器代码
public class MyConnectionManager implements ServletContextListener {
    Connection con = null;
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ssm_student", "root", "root");
            servletContextEvent.getServletContext().setAttribute("con",con);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        con = (Connection)servletContextEvent.getServletContext().getAttribute("con");
        try{
            con.close();
        }catch (SQLException e){
            e.printStackTrace();
        }
    }
}
2、数据库配置:jdbc.properties(数据库要有对应的值:建库建表加数据步骤省略
url=jdbc:mysql://localhost:3306/ssm_student
user=root
password=root
driver=com.mysql.jdbc.Driver
注意:把数据库驱动jar导入进libmysql-connector-java-5.1.37-bin.jar
3、web.xml配置(把监听器注册进去--注意路径)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <listener>
        <listener-class>Chapter08.Listener.MyConnectionManager</listener-class>
    </listener>
</web-app>
运行结果会显示所有的用户名和用户密码。

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

WEB前端

QQ|手机版|小黑屋|金桨网|助学堂  咨询请联系站长。

GMT+8, 2024-5-6 02:46 , Processed in 0.055413 second(s), 21 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表