Web.xml配置详解之listener
来源:互联网 发布:手机通话记录软件大全 编辑:程序博客网 时间:2024/06/07 02:30
转自:http://blog.csdn.net/liaoxiaohua1981/article/details/6760423
- 定义
- <listener>
- <listen-class>com.myapp.MyListener</listen-class>
- </listener>
- 作用
该元素用来注册一个监听器类。可以收到事件什么时候发生以及用什么作为响应的通知。事件监听程序在建立、修改和删除会话或servlet环境时得到通知。常与context-param联合使用。 - listen-class 指定监听类,该类继承ServletContextListener 包含初始化方法contextInitialized(ServletContextEvent event) 和 销毁方法contextDestoryed(ServletContextEvent event)
- 示例:初始化日志配置文件
Web.xml配置详解之listener
- <!--初始化日志配置文件 -->
- <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;
- /**
- * @author louisliao
- *
- */
- 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);
- }
- }
- }
0 0
- Web.xml配置详解之listener
- Web.xml配置详解之listener
- Web.xml配置详解之listener
- Web.xml配置详解之listener
- Web.xml配置之listener
- Web.xml配置详解之context-param,listener
- web.xml配置详解之listener与context-param
- web.xml 配置-listener
- web.xml配置详解 context-param listener init-param
- web.xml Listener 监听器配置
- java中web.xml中的<listener> 详解
- web.xml listener、filter、servlet详解
- web.xml里listener、filter配置
- j2ee中web.xml中配置listener
- java web.xml 配置session listener
- Web.xml配置详解之context-param
- web.xml配置详解之filter
- SSH之web.xml配置详解
- android 开发第六弹--View事件传递和碎片
- 使用Eclipse构建Maven的SpringMVC项目
- Java/Android 多少年月日时分秒前
- ubuntu 13.04安装supervisor
- PopMetal和PX2上运行upstream linux kernel代码
- Web.xml配置详解之listener
- 关于创建String对象过程的内存分配
- Ubuntu adb devices :???????????? no permissions 解决方法
- php后门的编写
- eclipse工程转maven工程出错的解决办法
- 黑马程序员学习(四) 代码块、this、static
- 产品经理被一群程序员围着撕逼的时候你该怎么办?
- Android广播机制总结
- symfony 学习 1 命令