美文网首页
JavaWeb 监听器

JavaWeb 监听器

作者: pinnuli | 来源:发表于2018-07-31 11:00 被阅读0次

    按监听的对象划分

    1.用于监听应用程序环境对象(ServletContext)的事件监听器,实现ServletContextListener接口

    public class MyServletContextListener implements ServletContextListener {
    
        public void contextInitialized(ServletContextEvent servletcontextevent) {
            String initParam = servletcontextevent.getServletContext().getInitParameter("initParam");
            System.out.println("contextInitialized : initParam = "+initParam);
        }
    
        public void contextDestroyed(ServletContextEvent servletcontextevent) {
            System.out.println("contextDestroyed");
        }
    
    }
    

    2.用于监听用户会话对象(HttpSession)的事件监听器,实现HttpSessionListener接口

    public class MyHttpSessionListener implements HttpSessionListener {
    
        public void sessionCreated(HttpSessionEvent httpsessionevent) {
            System.out.println("sessionCreated");
        }
    
        public void sessionDestroyed(HttpSessionEvent httpsessionevent) {
            System.out.println("sessionDestroyed");
        }
    
    }
    

    3.用于监听请求消息对象(ServletRequest)的事件监听器,实现ServletRequestListener接口

    public class MyServletRequestListener implements ServletRequestListener {
    
        public void requestDestroyed(ServletRequestEvent servletrequestevent) {
            System.out.println("requestDestroyed ");
        }
    
        public void requestInitialized(ServletRequestEvent servletrequestevent) {
            String name = servletrequestevent.getServletRequest().getParameter("name");
            System.out.println("requestInitialized name:"+name);
        }
    
    }
    

    按监听的事件划分

    1.监听域对象自身的创建和销毁的事件监听器

    即按监听对象划分的那几种

    2.监听域对象的属性的增加和删除的事件监听器,实现ServletContextAttributeListenerHttpSessionAttributeListenerServletRequestAttributeListener接口

    public class MyServletContextAttributeListener implements ServletContextAttributeListener {
    
        public void attributeAdded(ServletContextAttributeEvent servletcontextattributeevent) {
            System.out.println("ServletContext_attributeAdded:"+servletcontextattributeevent.getName());
        }
    
        public void attributeRemoved(ServletContextAttributeEvent servletcontextattributeevent) {
            System.out.println("ServletContext_attributeRemoved:"+servletcontextattributeevent.getName());
    
        }
    
        public void attributeReplaced(ServletContextAttributeEvent servletcontextattributeevent) {
            System.out.println("ServletContext_attributeReplaced:"+servletcontextattributeevent.getName());
    
        }
    
    }
    

    HttpSessionServletRequest 同理,只是方法参数类型不同

    3.监听绑定到HttpSession域中的某个对象的状态的事件监听器

    这种情况不需要专门设计一个作为监听器的类,可以作为一个实体类,然后继承需要的接口:

    public class User implements HttpSessionBindingListener,HttpSessionActivationListener,Serializable {
    
        private String username;
        
        public void valueBound(HttpSessionBindingEvent httpsessionbindingevent) {
            System.out.println("valueBound Name:"+httpsessionbindingevent.getName());
        }
    
        public void valueUnbound(HttpSessionBindingEvent httpsessionbindingevent) {
            System.out.println("valueUnbound Name:"+httpsessionbindingevent.getName());
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        //钝化
        public void sessionWillPassivate(HttpSessionEvent httpsessionevent) {
            System.out.println("sessionWillPassivate "+httpsessionevent.getSource());
        }
        //活化
        public void sessionDidActivate(HttpSessionEvent httpsessionevent) {
            System.out.println("sessionDidActivate "+httpsessionevent.getSource());
        }
    }
    

    绑定和解除绑定:实现HttpSessionBindingListener接口
    钝化和活化:实现HttpSessionActivationListenerSerializable接口

    实现Serializable接口是因为钝化时需要将seesion序列化存储到文件或者数据库,活化时需要反序列化

    参阅:
    慕课网:JAVA Web开发技术应用——监听器

    相关文章

      网友评论

          本文标题:JavaWeb 监听器

          本文链接:https://www.haomeiwen.com/subject/wevcvftx.html