[英]Property File doesnt work
I'm trying to find a way, how to use a properties file in my Java Servlet (extends http-servlet). 我正试图找到一种方法,如何在我的Java Servlet中使用属性文件(扩展http-servlet)。 I've tried out to use
ClassLoader#getResourceAsStream()
and ServletContext#getResourceAsStream()
. 我已经尝试使用
ClassLoader#getResourceAsStream()
和ServletContext#getResourceAsStream()
。 But whatever I'm doing, nothing works and there is always a NullPointerException
. 但无论我在做什么,什么都
NullPointerException
总有一个NullPointerException
。
database.properties
File: database.properties
文件:
Driver=org.postgresql.Driver
Protokoll=jdbc:postgresql://
Speicherort=localhost/
Datenbank=Ticketshop
User=postgres
code: 码:
p = new Properties();
p.load(getServletContext().getResourceAsStream("/WEB-INF/properties/database.properties"));
protokoll = p.getProperty("Protokoll");
speicherort = p.getProperty("Speicherort");
user = p.getProperty("User");
driver = p.getProperty("Driver");
password = p.getProperty("Password");
database = p.getProperty("Datenbank");
file-tree: 文件树:
Java Resources
|-- src
|-- login
|-- Login.java
WebContent
|-- WEB-INF
|-- properties
|-- database.properties
Why don't you use ResourceBundle
. 为什么不使用
ResourceBundle
。 It is so simple to use. 它使用起来非常简单。 Place properties file in the source folder , and
将属性文件放在源文件夹中 ,然后
import java.util.MissingResourceException;
import java.util.ResourceBundle;
public class DatabaseConstantsAccessor
{
// don't include .properties extension, just specify the name without extension
private static final String BUNDLE_NAME = "database";
private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME);
private ConstantsAccessor()
{
}
public static String getString(String key)
{
try
{
return RESOURCE_BUNDLE.getString(key);
}
catch (MissingResourceException e)
{
return '!' + key + '!';
}
}
}
And where you want to access properties, use following code: 在您要访问属性的位置,请使用以下代码:
String driverString=DatabaseConstantsAccessor.getString("Driver");
Integer intProp=Integer.valueOf(DatabaseConstantsAccessor.getString("SomeIntProperty"));
check all names. 检查所有名字。 your code is working fine.
你的代码工作正常。
Try this 尝试这个
Edit 编辑
p.load(getServletContext().getClassLoader().getResourceAsStream("properties/database.properties"));
And BTW you need to move the database.properties
to /WEB-INF/classes
folder for this to work 顺便说一句,你需要
move the database.properties
到/WEB-INF/classes
文件夹才能使用
The Folder structure should be 文件夹结构应该是
WEB-INF
| classes
|properties
database.properties
I agree with "dj aqeel" but to get a ResourceBundle, this must be in the class directory. 我同意“dj aqeel”但要获得ResourceBundle,这必须在类目录中。
Thus, I put the file in "/WEB-INF/ classes /properties/database.properties". 因此,我将文件放在“/ WEB-INF / classes /properties/database.properties”中。 Obviously, you must put the file in the src directory and the compiler move there automatically.
显然,您必须将文件放在src目录中,编译器会自动移动到那里。
Marcos 马科斯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.