zoukankan      html  css  js  c++  java
  • java web filter读取classpath配置文件内容

    以下demo,从类路径classpath中获取venus.properties(本项目中用到的文件),思路是在初始化的时候读取,然后放在局部变量里面。

    package club.codeapes.web.core.filter;
    
    
    import javax.servlet.*;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    
    public class WebRootPageFilter implements Filter {
    
        private String ssoOn;
    
        public void destroy() {
    
        }
    
        public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
            HttpServletRequest request = (HttpServletRequest) req;
            HttpServletResponse response = (HttpServletResponse) res;
            //String effectivePath = request.getRequestURI().substring(request.getContextPath().length());
            System.out.println(ssoOn);
            chain.doFilter(request, response);
        }
    
        public void init(FilterConfig filterConfig) throws ServletException {
            try {
                Properties configProperties = new Properties();
                InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("venus.properties");
                configProperties.load(inputStream);
                ssoOn = configProperties.getProperty("sso.on");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    在初始化init方法里面使用,一般情况下,这个方法只调用一次,初始化的时候调用。

  • 相关阅读:
    男孩的眼泪
    清冷
    Java随笔
    Java随笔
    skip a transaction in goldengate(跳过一个事务OGG)
    Oracle性能问题一般排查方法
    Oracle GoldenGate(ogg)安装经验大汇总,采坑总结,绝对干货!
    ORACLE 11G 性能诊断优化之ASH实战分析详解
    Oracle SQL性能优化40条 | 收藏了!
    GoldenGate OGG ORACLE数据复制实施方案
  • 原文地址:https://www.cnblogs.com/chenmz1995/p/10315300.html
Copyright © 2011-2022 走看看