【问题标题】:Load properties file in Servlet/JSP [duplicate]在 Servlet/JSP 中加载属性文件 [重复]
【发布时间】:2012-09-12 11:43:09
【问题描述】:

我从我的Java project 创建了一个jar,并希望在JSP Servlet Project 中使用同一个jar。我正在尝试从我的JSP Servlet Project 中加载一个属性文件,比如说sample.properties,它保存在WEB/properties/sample.properties 中,应该由jar 中的一个类读取。我正在使用下面的代码写在一个jar 类中访问它。

Properties prop=new Properties();
prop.load(/WEB-INF/properties/sample.properties);

但每次我收到fileNotFound exception。
请给我建议解决方案。

这是结构

WEB-INF
      |
       lib
          |
           myproject.jar
                       |
                        myclass (This class needs to read sample.properties)
      |
       properties
                 |sample.properties

【问题讨论】:

标签: java jsp servlets properties


【解决方案1】:

/WEB-INF 文件夹不是类路径的一部分。因此,这里任何暗示ClassLoader#getResourceAsStream() 的答案都将永远起作用。只有将属性文件放在确实是类路径的一部分的/WEB-INF/classes 中才会起作用(在像 Eclipse 这样的 IDE 中,只需将其放在 Java 源文件夹根目录中就足够了)。

如果属性文件确实在您想要保留的位置,那么您应该通过ServletContext#getResourceAsStream() 将其作为网络内容资源获取。

假设您在 HttpServlet 中,应该这样做:

properties.load(getServletContext().getResourceAsStream("/WEB-INF/properties/sample.properties"));

(getServletContext() 继承自 servlet 超类,您无需自己实现;因此代码保持原样)

但如果该类本身根本不是HttpServlet,那么您确实需要将属性文件移动到类路径中。

另见:

【讨论】:

  • 所有好的答案 - 魔鬼的拥护者通过;如果您打算将敏感信息存储在所述属性文件中(例如 API 用户名/密码/密钥等),那么它可能不应该在您的代码库中;因为那时它将受到版本控制——这只是人们获取信息的另一种方式;在这种情况下,将其存储在文档根目录之外
【解决方案2】:

尝试将sample.properties放到src文件夹下,然后

Properties prop = new Properties();
prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("myprop.properties"));

【讨论】:

    【解决方案3】:

    将您的属性文件移动到WEB-INF/classes 下。然后加载如下:

    prop.load(getClass().getResourceAsStream("sample.properties"));

    您也可以将其放入classes 下的子目录中。在这种情况下,相应地将呼叫更改为getResourceAsStream()。

    为了在多类加载器系统中更安全,您可以改用Thread.getContextClassLoader().getResourceAsStream()。

    要使属性文件到达你的war文件的classes文件夹,你必须把它放在你项目中的resources文件夹下(如果你使用maven),或者如果你不放在src文件夹下使用类似 Maven 的目录结构。

    【讨论】:

    • 我是用InputStream inStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("../properties/DBDetails.properties"); prop.load(inStream)做到的
    【解决方案4】:

    试试这个,

     InputStream inStream = Thread.currentThread().getContextClassLoader()
                         .getResourceAsStream("/WEB-INF/properties/sample.properties");
    

    然后,将其加载(InputStream)到一个 Properties 对象中:

    Properties props = new Properties();
    props.load(inStream);
    

    【讨论】:

    • 我试过了,但是没有用。它抛出了同样的异常。
    • 不应该是WEB-INF/properties/sample.properties而不是/WEB-INF/properties/sample.properties吗?
    • 将您的属性文件添加到您尝试访问该文件的 jar 文件中,即jar 文件中的项目。或者将 sample.properties 放在 C:/D: 或其他地方并给出完整路径,例如 C:/sample.properties。
    【解决方案5】:

    如果您尝试从 jsp/servlet 加载属性,它可能不起作用。编写一个实用程序类来读取属性和包以及 jar 文件。将属性文件复制到与实用程序相同的包中。

     Class Utility{
        Properties properties=null;
        public void load() throws IOException{
            properties.load(getClass().getResourceAsStream("sample.properties"));
        }
        public Object get(String key) throws IOException{
            if (properties==null){
                load();
            }
            return properties.get(key); 
        }
      }
    

    现在使用 servlet 中的这个实用程序类来读取属性值。也许您可以将类定义为单例以便更好地练习

    干杯 萨西斯

    【讨论】:

      猜你喜欢
      • 2013-09-01
      • 1970-01-01
      • 2018-06-30
      • 1970-01-01
      • 2023-03-03
      • 2011-04-24
      • 2016-01-04
      • 2012-09-23
      • 2012-11-18
      相关资源
      最近更新 更多