Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Servlet/JSP中加载属性文件_Java_Jsp_Servlets_Properties - Fatal编程技术网

Java 在Servlet/JSP中加载属性文件

Java 在Servlet/JSP中加载属性文件,java,jsp,servlets,properties,Java,Jsp,Servlets,Properties,我已经从我的Java项目创建了一个jar,并希望在jspservlet项目中使用相同的jar。我试图从保存在WEB/properties/sample.properties中的JSP Servlet项目中加载属性文件sample.properties,该文件应该由jar中的类读取。我在jar类中使用以下代码writen来访问它 Properties prop=new Properties(); prop.load(/WEB-INF/properties/sample.properties);

我已经从我的
Java项目创建了一个
jar
,并希望在
jspservlet项目中使用相同的jar
。我试图从保存在
WEB/properties/sample.properties
中的
JSP Servlet项目中加载属性文件sample.properties,该文件应该由
jar
中的类读取。我在jar类中使用以下代码writen来访问它

Properties prop=new Properties();
prop.load(/WEB-INF/properties/sample.properties);
但是每次我得到
filenotfoundexception

请给我建议解决办法

结构如下

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

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

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

 InputStream inStream = Thread.currentThread().getContextClassLoader()
                     .getResourceAsStream("/WEB-INF/properties/sample.properties");
然后,将其加载(InputStream)到Properties对象中:

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

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

prop.load(getClass().getResourceAsStream(“sample.properties”)

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

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


要使属性文件到达war文件的
classes
文件夹,必须将其放在项目的
resources
文件夹下(如果您使用的是maven),或者如果您不使用类似maven的目录结构,则必须将其放在
src
文件夹下

文件夹
/WEB-INF
不是类路径的一部分。因此,这里任何未经深思熟虑的建议都不会奏效。只有当属性文件放在类路径的一部分时(在像Eclipse这样的IDE中,只要把它放在Java源文件夹根目录中就足够了),它才会起作用

如果属性文件确实在您想要保存它的地方,那么您应该将其作为web内容资源来获取

假设您在一个
HttpServlet
中,应该执行以下操作:

properties.load(getServletContext().getResourceAsStream("/WEB-INF/properties/sample.properties"));
(getServletContext()的
getServletContext()
是从servlet超类继承的,您不需要自己实现它;因此代码就是这样)

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

另见:

如果试图从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中的这个实用程序类来读取属性值。可能您可以将类定义为singleton,以便更好地实践

干杯
Satheesh

我尝试了这个,但没有成功。它引发了相同的异常。它不应该是
WEB-INF/properties/sample.properties
而不是
/WEB-INF/properties/sample.properties
?将属性文件添加到您试图访问该文件的jar文件中,i、 e.jar文件中的项目。或者将sample.properties放在C:/D:或其他地方,并给出完整路径,例如C:/sample.properties.check,我使用
InputStream inStream=Thread.currentThread().getContextClassLoader().getResourceAsStream(“../properties/DBDetails.properties”);道具负载(河道内)