Web.xml配置详解之listener

转自:http://blog.csdn.net/liaoxiaohua1981/article/details/6760423
  • 定义
[html]  view plain copy
  1. <listener>  
  2. <listen-class>com.myapp.MyListener</listen-class>  
  3. </listener>  
  • 作用
    该元素用来注册一个监听器类。可以收到事件什么时候发生以及用什么作为响应的通知。事件监听程序在建立、修改和删除会话或servlet环境时得到通知。常与context-param联合使用。
  • listen-class 指定监听类,该类继承ServletContextListener 包含初始化方法contextInitialized(ServletContextEvent event) 和 销毁方法contextDestoryed(ServletContextEvent event)
  • 示例:初始化日志配置文件
[html]  view plain copy
  1. <!--初始化日志配置文件 -->    
  2. <listener>    
  3.     <listener-class>    
  4.         com.myapp.LogbackConfigListener    
  5.     </listener-class>    
  6. </listener>    
  7. <context-param>    
  8.     <param-name>logbackConfigLocation</param-name>    
  9.     <param-value>WEB-INF/logback.xml</param-value>    
  10. </context-param>    
[java]  view plain copy
  1. /** 
  2.  *  
  3.  */  
  4. package com.myapp;  
  5.   
  6. import javax.servlet.ServletContextEvent;  
  7. import javax.servlet.ServletContextListener;  
  8. import org.slf4j.Logger;    
  9. import org.slf4j.LoggerFactory;    
  10. import ch.qos.logback.classic.LoggerContext;    
  11. import ch.qos.logback.classic.joran.JoranConfigurator;    
  12. import ch.qos.logback.core.joran.spi.JoranException;   
  13. /** 
  14.  * @author louisliao 
  15.  * 
  16.  */  
  17. public class LogbackConfigListener implements ServletContextListener {  
  18.   
  19.     private static final Logger logger = LoggerFactory.getLogger(LogbackConfigListener.class);    
  20.     private static final String CONFIG_LOCATION = "logbackConfigLocation";    
  21.     /* (non-Javadoc) 
  22.      * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent) 
  23.      */  
  24.     public void contextDestroyed(ServletContextEvent arg0) {  
  25.         // TODO Auto-generated method stub  
  26.   
  27.     }  
  28.   
  29.     /* (non-Javadoc) 
  30.      * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent) 
  31.      */  
  32.     public void contextInitialized(ServletContextEvent event) {  
  33.         // TODO Auto-generated method stub  
  34.         String logbackConfigLocation = event.getServletContext().getInitParameter(CONFIG_LOCATION);    
  35.         String fn = event.getServletContext().getRealPath(logbackConfigLocation);    
  36.         try {    
  37.             LoggerContext loggerContext = (LoggerContext)LoggerFactory.getILoggerFactory();    
  38.             loggerContext.reset();    
  39.             JoranConfigurator joranConfigurator = new JoranConfigurator();    
  40.             joranConfigurator.setContext(loggerContext);    
  41.             joranConfigurator.doConfigure(fn);    
  42.             logger.debug("loaded slf4j configure file from {}", fn);    
  43.         }    
  44.         catch (JoranException e) {    
  45.             logger.error("can loading slf4j configure file from " + fn, e);    
  46.         }    
  47.   
  48.     }  
  49.   
  50. }  
Web.xml配置详解之listener
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值