Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use property file in jsp [duplicate]

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?

like image 844
KhAn SaAb Avatar asked Oct 02 '12 07:10

KhAn SaAb


People also ask

How to find duplicate keys in properties file?

Simple and easy way..get codePro AnalytiX from google, its a eclipse plugin. You can audit your code, it will find all the duplicate key in properties file.

How to Set property in properties file in Java?

To set properties in a Java Properties instance you use the setProperty() method. Here is an example of setting a property (key - value pair) in a Java Properties object: properties.

How get value from properties file in JSP?

In order to get your properties file, you could use the helpful method in ServletContext: getResourceAsStream. getValuesFromPropertiesFile(request, "my_properties. txt" ); It could be noted that the Properties class does implement the Map interface, and you can pass it straight back to your JSP for read-only purposes.


2 Answers

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%>
like image 196
5 revs, 4 users 59%user1697238 Avatar answered Sep 29 '22 05:09

5 revs, 4 users 59%user1697238


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.

like image 42
AlexR Avatar answered Sep 29 '22 05:09

AlexR