JavaWeb——监听器

导读:本篇文章讲解 JavaWeb——监听器,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

监听器介绍

  • 监听器(Listener) 是J2EE Servlet模块下的组件

  • Listener的作用对Web应用对象的行为进行监控

  • 通过Listener监听自动触发指定的功能代码

1、Listener 监听器它是 JavaWeb 的三大组件之一。JavaWeb 的三大组件分别是:Servlet 程序、Filter 过滤器、Listener 监 听器。

2、Listener 它是 JavaEE 的规范,就是接口

3、监听器的作用是,监听某种事物的变化。然后通过回调函数,反馈给客户(程序)去做一些相应的处理。

三种监听对象

  • ServletContext -对全局ServletContext及其属性进行监听

  • HttpSession -对用户会话及其属性操作进行监听

  • ServletRequest -对请求及属性操作进行监听

第一个监听器

开发监听器三要素

  • 实现XxxListener接口,不同接口对应不同监听对象

  • 实现每个接口中独有的方法,实现触发监听的后续操作

  • 在web.xml中配置< listener>使监听器生效

public class FirstListener implements ServletContextlistener {
    @Override
    public void contextInitialized (ServletContextEvent sce) {
    	// TODO Auto-generated method stub
        System.out.println("ServletContext已初始化"),
        
    }
    @Override
    public void contextDestroyed (ServletContextEvent sce) {
    	// TODO Auto-generated method stub
        System.out.println ("ServletContext已销毁"),
    }
}

xml.web中编写监听器对应的类是哪个

<listener>
	<1istener-class>com.imooc.listener.FirstListener</listener-class>
</listener>

注解形式:

@WebListener
public class FirstListener implements ServletContextlistener {
    @Override
    public void contextInitialized (ServletContextEvent sce) {
    	// TODO Auto-generated method stub
        System.out.println("ServletContext已初始化"),
        
    }
    @Override
    public void contextDestroyed (ServletContextEvent sce) {
    	// TODO Auto-generated method stub
        System.out.println ("ServletContext已销毁"),
    }
}

内置对象监听器接口

  • ServletContextListener -监听ServletContext对象创建、销毁等操作

  • HttpSessionListener -监听HttpSession对象创建、销毁等操作

  • ServletRequestListener -监听HttpServletRequest对象创建、销毁等

public class FirstListener implements ServletContextlistener,HttpSessionListener,ServletRequestListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
    	// TODO Auto-generated method stub
        System.out.println("ServletContext已初始化"),
    }
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
    	// TODO Auto-generated method stub
        System.out.println ("ServletContext已销毁");
    }
    
    @override
	public void sessionCreated(HttpSessionEvent se) {
		HttpSession session = se.getSession();
        // TODO Auto-generated method stub
        system.out.println ("Session已被创建,SessionId:" + session.getId());
	}
    @averride
    public void sessionDestroyed(HttpSessionEvent se)(
		// TODO Auto-generated method stub
        System.out.println ("Session已销毁");
	}
        
        @override
        public void requestDestroyed (ServletRequestEvent sre) {
        // TODO Auto-generated method stub
        System.out.println ("Request已销毁");
    }
        @Override
        public void requestInitialized (ServletRequestEvent sre) {
        ServletRequest request = (HttpServletRequest) sre.getServletRequest() ;
        // TODO” Auto-generated method stub
        system.out.println("HttpServletRequest已被创建,uri:" + request .getRequestURI());   
    }

属性监听接口

  • ServletContextAttributeListener -监听全局属性操作

  • HttpSessionAttributeListener-监听用户会话属性操作

  • ServletRequestAttributeListener -监听请求属性操作

Servlet:

public class test extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().println("Hello World!") ;
        request.getServletContext().setAttribute ("sc-attr1", "sc-attr-val");
        request.getSession().setAttribute("session-attr1", "session-attr-val");
        request. setAttribute ("request-attr1","request-attr-value1") ;
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}

Listener:

@WebListener
public class WebAttributeListener implements ServletContextAttributeListener, HttpSessionAttributeListener, ServletRequestAttributeListener {

    public void attributeAdded(ServletContextAttributeEvent event) {
        System.out.println("ServletContext新增属性:"+event.getName()+"->"+event.getValue());
    }

    public void attributeRemoved(ServletContextAttributeEvent event) {

    }

    public void attributeReplaced(ServletContextAttributeEvent event) {

    }


    public void attributeAdded(ServletRequestAttributeEvent event) {
        System.out.println("ServletRequest新增属性:"+event.getName()+"->"+event.getValue());
    }

    public void attributeRemoved(ServletRequestAttributeEvent event) {
        
    }

    public void attributeReplaced(ServletRequestAttributeEvent event) {

    }


    public void attributeAdded(HttpSessionBindingEvent event) {
        System.out.println("HttpSession新增属性:"+event.getName()+"->"+event.getValue());
    }

    public void attributeRemoved(HttpSessionBindingEvent event) {

    }

    public void attributeReplaced(HttpSessionBindingEvent event) {

    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/99481.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!