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方法里面使用,一般情况下,这个方法只调用一次,初始化的时候调用。

  • 相关阅读:
    Python元组、列表、字典
    测试通过Word直接发布博文
    Python环境搭建(windows)
    hdu 4003 Find Metal Mineral 树形DP
    poj 1986 Distance Queries LCA
    poj 1470 Closest Common Ancestors LCA
    poj 1330 Nearest Common Ancestors LCA
    hdu 3046 Pleasant sheep and big big wolf 最小割
    poj 3281 Dining 最大流
    zoj 2760 How Many Shortest Path 最大流
  • 原文地址:https://www.cnblogs.com/chenmz1995/p/10315300.html
Copyright © 2011-2022 走看看