当前位置:首页 > 开发 > 编程语言 > XML > 正文

web.xml配置详解之listener

发表于: 2014-02-10   作者:bijian1013   来源:转载   浏览:
摘要: 一.定义 <listener> <listen-class>com.myapp.MyListener</listen-class> </listener>   二.作用        该元素用来注册一个监听器类。可以收到事件什么时候发生以及用什么作为响

一.定义

<listener>  
	<listen-class>com.myapp.MyListener</listen-class>  
</listener>

 

二.作用
        该元素用来注册一个监听器类。可以收到事件什么时候发生以及用什么作为响应的通知。事件监听程序在建立、修改和删除会话或servlet环境时得到通知。常与context-param联合使用。

 

三.listen-class 指定监听类,该类继承ServletContextListener 包含初始化方法contextInitialized(ServletContextEvent event) 和 销毁方法contextDestoryed(ServletContextEvent event)

 

四.示例:初始化日志配置文件

<!--初始化日志配置文件 -->    
<listener>    
    <listener-class>    
        com.myapp.LogbackConfigListener    
    </listener-class>    
</listener>    
<context-param>    
    <param-name>logbackConfigLocation</param-name>    
    <param-value>WEB-INF/logback.xml</param-value>    
</context-param>

 

package com.myapp;  
  
import javax.servlet.ServletContextEvent;  
import javax.servlet.ServletContextListener;  
import org.slf4j.Logger;    
import org.slf4j.LoggerFactory;    
import ch.qos.logback.classic.LoggerContext;    
import ch.qos.logback.classic.joran.JoranConfigurator;    
import ch.qos.logback.core.joran.spi.JoranException;   
/** 
 * 
 */  
public class LogbackConfigListener implements ServletContextListener {  
  
    private static final Logger logger = LoggerFactory.getLogger(LogbackConfigListener.class);    
    private static final String CONFIG_LOCATION = "logbackConfigLocation";    
    /* (non-Javadoc) 
     * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent) 
     */  
    public void contextDestroyed(ServletContextEvent arg0) {  
        // TODO Auto-generated method stub   
  
    }  
  
    /* (non-Javadoc) 
     * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent) 
     */  
    public void contextInitialized(ServletContextEvent event) {  
        // TODO Auto-generated method stub   
        String logbackConfigLocation = event.getServletContext().getInitParameter(CONFIG_LOCATION);    
        String fn = event.getServletContext().getRealPath(logbackConfigLocation);    
        try {    
            LoggerContext loggerContext = (LoggerContext)LoggerFactory.getILoggerFactory();    
            loggerContext.reset();    
            JoranConfigurator joranConfigurator = new JoranConfigurator();    
            joranConfigurator.setContext(loggerContext);    
            joranConfigurator.doConfigure(fn);    
            logger.debug("loaded slf4j configure file from {}", fn);    
        }    
        catch (JoranException e) {    
            logger.error("can loading slf4j configure file from " + fn, e);    
        }    
    }
}

 

文章来源:http://blog.csdn.net/liaoxiaohua1981/article/details/6760423

web.xml配置详解之listener

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号