码迷,mamicode.com
首页 > 编程语言 > 详细

JavaWeb Listener

时间:2016-04-26 23:48:07      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:

1 ServletContextListener

   监听ServletContext对象被创建和销毁的Servlet监听器

   开发:>创建一个实现了ServletContextListener的类并且实现里面的两个方法

              public class HelloServletContextListener implements ServletContextListener 

public class HelloServletContextListener implements ServletContextListener {

    public void contextDestroyed(ServletContextEvent sce) {
        // TODO Auto-generated method stub
        System.out.println("销毁");
        
    }

    public void contextInitialized(ServletContextEvent sce) {
        // TODO Auto-generated method stub
        System.out.println("创建");
        
    }

 

            >并在web.xml里配置该Listener

             

<!-- 配置listener -->
    <listener>
    <listener-class>cn.stud.wlc.listener.HelloServletContextListener</listener-class>
    </listener>

           >ServletContextListener最常用在web应用加载时对当前web应用的相关资源进行初始化操作(创建数据库链接池。。。)

技术分享

(2,3)ServletRequestListener& HttpSessionListener和ServletContextListener类似

public class HelloServletContextListener implements ServletContextListener,HttpSessionListener,ServletRequestListener {

    public void contextDestroyed(ServletContextEvent sce) {
        // TODO Auto-generated method stub
        System.out.println("销毁");
        
    }

    public void contextInitialized(ServletContextEvent sce) {
        // TODO Auto-generated method stub
        System.out.println("创建");
        
    }

    public void requestDestroyed(ServletRequestEvent sre) {
        // TODO Auto-generated method stub
        
    }

    public void requestInitialized(ServletRequestEvent sre) {
        // TODO Auto-generated method stub
        
    }

    public void sessionCreated(HttpSessionEvent se) {
        // TODO Auto-generated method stub
        
    }

    public void sessionDestroyed(HttpSessionEvent se) {
        // TODO Auto-generated method stub
        
    }

}

 

JavaWeb Listener

标签:

原文地址:http://www.cnblogs.com/wlc297984368/p/5436924.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!