19

Possible Duplicate:
How to load a .properties file into a jsp

I am trying to use my property file in JSP but it wont work properly and throws an error

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

Here I am trying to access my property file:

<%
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());
}

I have tried by many ways to access property file. How do I fix this?

Community
  • 1
  • 1
KhAn SaAb
  • 5,248
  • 5
  • 31
  • 52

3 Answers3

34

create test.properties file in your package

pname=xyz
psurname=abc

create jsp file:

<%@ 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%>
KhAn SaAb
  • 5,248
  • 5
  • 31
  • 52
5

JSP runs in servlet container, so its current working directory is defined by the container. Typically it is the directory where container is installed or its bin directory. In any case it is not the place where you want to store your custom properties file.

There are 2 typical approaches to do what you need.

The first approach is good if your file is a part of your application and you never change it on deployment. In this case read it from resources:

props.load(getClass().getResourceAsStream())

or even better

props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream())

The second approach is good if you want to change your properties file on deployment environment. In this case put it somewhere in the file system outside your container. For example /opt/mycompany/conf/myproperties.properties on linux or at any other location you like. Now you should use absolute path when creating FileInputStream.

To make system better configurable you should not write the path to configuration file inside the code. Better approach is to pass it to application using system properties, e.g. add parameter like -Dmycompany.conf=/opt/mycompany/myprops.properties when you are running your application server. When you want to read the file do the following:

new FileInputStream(System.getProperties("mycompany.conf"))

Now configuration of your system can controlled independently by deployer.

AlexR
  • 114,158
  • 16
  • 130
  • 208
0

Try changing FileInputStream fis = new FileInputStream("myproperties.properties"); to FileInputStream fis = new FileInputStream(new File("myproperties.properties"));

Also, make sure that the classes that you are using has already be imported in your jsp file. Namely,

<%@ page import="java.io.FileInputStream" %>
<%@ page import="java.io.File" %>
Russell Gutierrez
  • 1,372
  • 8
  • 19