• Web中Listener的创建


    使用Listener只需要两个步骤:

    • 定义Listener实现类。
    • 通过Annotation或在web.xml文件中配置Listener

    实现Listener类

      监听不同Web事件的监听器不相同,常用的Web事件监听器接口有如下几个:

    • ServletContextListener:用于监听Web应用的启动和关闭。
    • ServletContextAttributeListener:用于监听ServletContext范围(application)内属性的改变。
    • ServletRequestListener:用于监听用户请求。
    • ServletRequestAttributeListener:用于监听ServletRequest范围(request)内属性的改变。
    • HttpSessionListener:用于监听用户session的开始和结束。
    • HttpSessionAttributeListener:用于监听HttpSesssion范围(session)内属性的改变。

      以ServletContextListener为例,要实现这个接口,需要实现两个方法:

    •   contextInitialized(ServletCOntextEvent sce):启动web应用时,系统调用Listener的该方法。
    •   contextDestroyed(ServletContextEvent sce):关闭web应用时,系统调用Listener的该方法。

      ServletContextListener的作用有点类似于load-on-startup Servlet,都可用于在Web应用启动时,回调方法来启动某些后台程序,这些后台程序负责为系统运行提供支持。

      例:创建一个Listener,用于在启动时获取数据库连接,设置到application中,关闭应用时,关闭连接。

    @WebListener
    public class GetConnListener implements ServletContextListener{
        //应用启动是,该方法被调用
        public void contextInitialized(ServletContextEvent sce){
            try{
                //取得应用的ServletContext实例
                ServletContext application = sce.getServletContext();
                //从配置参数中获取数据库信息
                String driver = application.getInitParameter("driver");
                String url = application.getInitParameter("url");
                String user = application.getInitParameter("user");
                String pass = application.getInitParameter("pass");
                //注册驱动
                Class.forName(driver);
                //获取数据库连接
                Connection conn = DriverManager.getConnection(url,user,pass);
                //将数据库连接设置成application范围内的属性
                application.setAttribute("conn",conn);
            }catch(Excetpion e){
                e.printStackTrace();
            }
        }
    
        //应用关闭时,该方法被调用
        public void contextDestroyed(ServletContextEvent sce){
            //取得该应用的ServletContext实例
            ServletContext application = sce.getServletContext();
            Connection conn = (Connection)application.getAttribute("conn");
            //关闭数据库连接
            if(conn!=null){
                try{
                    conn.close();
                }catch(SQLException e){
                    e.printStackTrace();
                }
            }
        }
    }

    配置Listener

      配置Listener只要向Web应用注册Listener实现类即可,无需配置参数之类的东西。为Web应用配置Listener有两种方式:

    • 使用@WebListener修饰Listener实现类。
    • 在web.xml文档中使用<listener.../>元素进行配置。

      使用@WebListener时通常无需指定任何属性,只要使用该Annotation修饰Listener实现类即可向Web应用注册该监听器。

      在web.xml中使用<listener.../>元素进行配置时只要配置其实现类即可,如:

    <listener>
        <!-- 指定Listener的实现类 -->
        <listener-class>luxl.GetGonnListener</listener-class>
    </listener>

    使用ServletContextAttributeListener

      ServletContextAttributeListener用于监听ServletContext(application)范围内属性的变化,实现该接口的监听器需要实现三个方法:

    • attributeAdded(ServletCOntextAttributeEvent event);当程序把一个属性存入application范围时出发该方法。
    • attributeRemoved(ServletContextAttributeEvent event);当程序把一个属性从application范围删除时出发该方法。
    • attributeReplaced(ServletContextAttributeEvent event);当程序替换application范围内的属性时将出发该方法。

    使用ServletRequestListener和ServletRequestAttributeListener

      ServletRequestListener用于监听用户请求的到达,实现该接口的监听器需要实现如下两个方法:

    • requestInitialized(ServletRequestEvent sre);用户请求到底、被初始化是出发该方法。
    • requestDestroyed(ServletRequestEvent sre); 用户请求结束、被销毁时触发该方法。

      ServletRequestAttributeListener则用于监听ServletRequest(request)范围内属性的变化,实现该接口的监听器需要实现attributeAdded、attributeRemoved、attributeReplaced三个方法。

      应用程序完全可以采用一个监听器来监听多个事件,只要让该监听器实现类同时实现多个监听器接口即可,不如同时实现ServletRequestListener、ServletRequesetAttributeListener。

      使用HttpSessionListener和HttpSessionAttributeListener与使用ServletRequestListener和ServletRequestAttributeListener非常相似。

      HttpSessionListener用于监听用户session的创建和销毁,实现该接口的监听器需要实现:

    •   sessionCreate(HttpSessionEvent se);用户与服务器的会话开始、创建时触发该方法。
    •   sessionDestroyed(HttpSessionEvent se);用户与服务器的会话断开、销毁时触发该方法。
  • 相关阅读:
    动态规划之矩阵连乘
    常见的开放符号服务器
    QT中的宏定义
    QT Creator项目路径设置
    批处理-日常小功能用法记录
    Qt Creator快捷键记录
    利用Navicat premium实现将数据从Oracle导入到MySQL
    php BCmath 封装类
    PHP 反射类
    Html标签生成类
  • 原文地址:https://www.cnblogs.com/ScorchingSun/p/4040949.html
Copyright © 2020-2023  润新知