小编典典

如何在jsp中使用属性文件

jsp

我正在尝试在JSP中使用我的属性文件,但无法正常工作并引发错误

Error: myproperties.properties (The system cannot find the file specified)

在这里,我试图访问我的属性文件:

<%
try
{
    FileInputStream fis = new FileInputStream("myproperties.properties");

    Properties p = new Properties();
    p.load(fis);
    String LogFileName = p.getProperty("NameOfLogFile");
    out.println(LogFileName);

}
catch (Exception e)
{// Catch exception if any
    out.println(e.getMessage());
}

我已经尝试了多种方法来访问属性文件。我该如何解决?


阅读 441

收藏
2020-06-08

共1个答案

小编典典

在包中创建test.properties文件

pname=xyz
psurname=abc

创建jsp文件:

<%@ page language="java" import="java.util.*" %> 
<%@ page import = "java.util.ResourceBundle" %>
<% ResourceBundle resource = ResourceBundle.getBundle("test");
  String name=resource.getString("pname");
  String surname=resource.getString("psurname"); %>
  <%=name %>
 <%=surname%>
2020-06-08