我其实有PROGRAMM与servlet:Servlet的init和
@WebServlet("/Controler")
public class Controler extends HttpServlet {
}
我需要使用属性文件:file.properties
在我的计划。加载它,我有一个类:
public class PropLoader {
private final static String m_propertyFileName = "file.properties";
public static String getProperty(String a_key){
String l_value = "";
Properties l_properties = new Properties();
FileInputStream l_input;
try {
l_input = new FileInputStream(m_propertyFileName); // File not found exception
l_properties.load(l_input);
l_value = l_properties.getProperty(a_key);
l_input.close();
} catch (Exception e) {
e.printStackTrace();
}
return l_value;
}
}
我的属性文件是在WebContent文件夹,我可以访问它:
String path = getServletContext().getRealPath("/file.properties");
但我不能调用其他类论文的方法比servlet ...
我如何访问PropLoader类中的属性文件?
好吧,一种选择是将路径作为静态变量添加到PropLoader类(单类的一种)。我已经看到了一些主要servlet在init()方法中执行这些步骤的情况,因此您将在整个应用程序中提供您的路径。你只需要确定你正在处理的servlet是在应用程序启动时加载的。 – Martin
我试过这个解决方案,但servlet无法在propLoader类中实例化路径,我认为这是由于init()servlet方法 – Apaachee