java自定义监听器

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * 监听application对象的创建和销毁
 */
public class SystemUpInitListener implements ServletContextListener {
    Logger logger= LoggerFactory.getLogger(SystemUpInitListener.class);

    //当application初始化
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        //获取application
        ServletContext application = sce.getServletContext();
        //获取上下文路径
        String contextPath = application.getContextPath();
        logger.debug("当前上下文路径contextPath{}",contextPath);
        application.setAttribute("PATH",contextPath);
    }

    //application 销毁
    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}

web.xml

<!--自定义监听器 监听上下文路径-->
  <listener>
    <listener-class>com.ytkj.rose.listener.SystemUpInitListener</listener-class>
  </listener>

jsp 页面运用

猜你喜欢

转载自www.cnblogs.com/yscec/p/12374426.html