zoukankan      html  css  js  c++  java
  • Listener

    ServletContextListener:监听web的启动和关闭。

    package com.li.test;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    import javax.servlet.annotation.WebListener;
    
    /**
     * Application Lifecycle Listener implementation class ListerTest_1
     * 
     */
    @WebListener
    public class ListerTest_1 implements ServletContextListener {
    
        /**
         * Default constructor.
         */
        public ListerTest_1() {}
    
        /**
         * @see ServletContextListener#contextInitialized(ServletContextEvent)
         */
        public void contextInitialized(ServletContextEvent event) {
    
            ServletContext context = event.getServletContext();
    
            String driver = context.getInitParameter("driver");
            String username = context.getInitParameter("username");
            String password = context.getInitParameter("password");
            String url = context.getInitParameter("url");
    
            try {
    
                Class.forName(driver);
                Connection conn = DriverManager.getConnection(url, username, password);
                context.setAttribute("conn", conn);
    
            } catch (Exception e) {
                System.out.println("connect error: " + e.getMessage());
                e.printStackTrace();
            }
    
        }
    
        /**
         * @see ServletContextListener#contextDestroyed(ServletContextEvent)
         */
        public void contextDestroyed(ServletContextEvent event) {
    
            ServletContext context = event.getServletContext();
            Connection conn = (Connection) context.getAttribute("conn");
    
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    
    }

    ServletContextAttributeListener:监听application范围内的属性的变化。

    package com.li.test;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletContextAttributeEvent;
    import javax.servlet.ServletContextAttributeListener;
    import javax.servlet.annotation.WebListener;
    
    /**
     * Application Lifecycle Listener implementation class ListerTest_2
     * 
     */
    @WebListener
    public class ListerTest_2 implements ServletContextAttributeListener {
    
        /**
         * Default constructor.
         */
        public ListerTest_2() {}
    
        /**
         * @see ServletContextAttributeListener#attributeAdded(ServletContextAttributeEvent)
         */
        public void attributeAdded(ServletContextAttributeEvent event) {
            
            ServletContext application = event.getServletContext();
            
            String name = event.getName();
            Object value = event.getValue();
            
            System.out.println(application+"范围内添加了属性:"+name+",值为:"+value);
        }
    
        /**
         * @see ServletContextAttributeListener#attributeReplaced(ServletContextAttributeEvent)
         */
        public void attributeReplaced(ServletContextAttributeEvent event) {
            
    ServletContext application = event.getServletContext();
            
            String name = event.getName();
            Object value = event.getValue();
            
            System.out.println(application+"范围内删除了属性:"+name+",值为:"+value);
        }
    
        /**
         * @see ServletContextAttributeListener#attributeRemoved(ServletContextAttributeEvent)
         */
        public void attributeRemoved(ServletContextAttributeEvent event) {
            
    ServletContext application = event.getServletContext();
            
            String name = event.getName();
            Object value = event.getValue();
            
            System.out.println(application+"范围内修改了属性:"+name+",值为:"+value);
        }
    
    }

    HttpSessionListener:监听session的开始和结束。

    HttpSessionAttributeListener

    HttpSessionActivationListener

    HttpSessionBindingListener

    package com.li.test;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import javax.servlet.ServletContext;
    import javax.servlet.annotation.WebListener;
    import javax.servlet.http.HttpSession;
    import javax.servlet.http.HttpSessionAttributeListener;
    import javax.servlet.http.HttpSessionBindingEvent;
    import javax.servlet.http.HttpSessionEvent;
    import javax.servlet.http.HttpSessionListener;
    
    /**
     * Application Lifecycle Listener implementation class ListerTest_4
     * 
     */
    @WebListener
    public class ListerTest_4 implements HttpSessionListener, HttpSessionAttributeListener {
    
        /**
         * Default constructor.
         */
        public ListerTest_4() {}
    
        /**
         * @see HttpSessionAttributeListener#attributeRemoved(HttpSessionBindingEvent)
         */
        public void attributeRemoved(HttpSessionBindingEvent arg0) {}
    
        /**
         * @see HttpSessionAttributeListener#attributeAdded(HttpSessionBindingEvent)
         */
        public void attributeAdded(HttpSessionBindingEvent arg0) {}
    
        /**
         * @see HttpSessionAttributeListener#attributeReplaced(HttpSessionBindingEvent)
         */
        public void attributeReplaced(HttpSessionBindingEvent arg0) {}
    
        /**
         * @see HttpSessionListener#sessionCreated(HttpSessionEvent)
         */
        public void sessionCreated(HttpSessionEvent sv) {
    
            HttpSession session = sv.getSession();
            ServletContext application = session.getServletContext();
            String sessionId = session.getId();
    
            if (session.isNew()) {
    
                String user = (String) session.getAttribute("user");
                user = (user == null) ? "游客" : user;
    
                Map<String, String> online = (Map<String, String>) application.getAttribute("online");
                if (online == null) {
                    online = new HashMap<String, String>();
                    online.put(sessionId, user);
                }
                application.setAttribute("online", online);
            }
        }
    
        /**
         * @see HttpSessionListener#sessionDestroyed(HttpSessionEvent)
         */
        public void sessionDestroyed(HttpSessionEvent sv) {
    
            HttpSession session = sv.getSession();
            ServletContext application = session.getServletContext();
            String sessionId = session.getId();
            Map<String, String> online = (Map<String, String>) application.getAttribute("online");
    
            if (online != null) {
                online.remove(sessionId);
            }
            application.setAttribute("online", online);
        }
    
    }

    ServletRequestListener

    package com.li.test;
    
    import javax.servlet.ServletRequestEvent;
    import javax.servlet.ServletRequestListener;
    import javax.servlet.annotation.WebListener;
    import javax.servlet.http.HttpServletRequest;
    
    /**
     * Application Lifecycle Listener implementation class ListerTest_3
     * 
     */
    @WebListener
    public class ListerTest_3 implements ServletRequestListener {
    
        /**
         * Default constructor.
         */
        public ListerTest_3() {}
    
        /**
         * @see ServletRequestListener#requestDestroyed(ServletRequestEvent)
         */
        public void requestDestroyed(ServletRequestEvent event) {
    
            HttpServletRequest request = (HttpServletRequest) event.getServletRequest();
            System.out.println(request);
        }
    
        /**
         * @see ServletRequestListener#requestInitialized(ServletRequestEvent)
         */
        public void requestInitialized(ServletRequestEvent event) {
    
            HttpServletRequest request = (HttpServletRequest) event.getServletRequest();
            System.out.println(request);
        }
    
    }

    ServletRequestAttributeListener

  • 相关阅读:
    数据库MySQL常用命令复习
    09、C语言——构造类型
    06、C语言——数组
    08、C语言——指针
    11、排序【理论篇】
    01、C语言——基础知识
    16、自学——Linux的学习进度与任务【vim编辑器及文件测试】
    15、自学——Linux的学习进度与任务【bash编程之条件判断】
    05、自学——计算机网络学习任务与进度(数据链路层)
    14、自学——Linux的学习进度与任务【正则表达式】
  • 原文地址:https://www.cnblogs.com/harryV/p/3678032.html
Copyright © 2011-2022 走看看