⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 contextlistener.java

📁 《JSP网站开发典型模块与实例精讲》一书光盘源码
💻 JAVA
字号:
package org.appfuse.webapp.listener;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import org.apache.log4j.xml.DOMConfigurator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.appfuse.webapp.filter.AuthFilter;

public class ContextListener
    extends HttpServlet
    implements
    ServletContextListener {
    private static Log log = LogFactory.getLog(ContextListener.class);

    /**
     * web应用启动的时候会执行,方法里面可以初始化配置文件,启动线程等初始化操作
     * @param sce ServletContextEvent
     */
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext servletContext = sce.getServletContext();
        Enumeration enumeration = servletContext.getInitParameterNames();
        String key = null;
        String value = null;
        while (enumeration.hasMoreElements()) {
            key = (String) enumeration.nextElement();
            value = servletContext.getInitParameter(key);
            if (key.equals("log")) {
                DOMConfigurator.configureAndWatch(getConfigPath(value),
                                                  60 * 1000);
            } else if(key.equals("unprotectedurls")){
                //to load unprotected parameters
               try{
                   String configPath = getConfigPath(value);
                   AuthFilter.load(configPath);
                   log.debug("unprotected resource infomation has loaded.");
               } catch(Exception ex){
                   log.error("Failed to load unprotected resource infomation ",ex);
               }
           }

        }
    }

    /**
     * web应用shut down的时候会执行,可以完成结束线程,清理资源等操作
     * @param sce ServletContextEvent
     */
    public void contextDestroyed(ServletContextEvent sce) {
    }

    public static String getConfigPath(String file) {
        String realPath = "";
        try {
            realPath = new File(file).getCanonicalPath();
        }
        catch (IOException ex) {
        }
        log.debug("the file is :" + realPath);
        return realPath;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -