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?
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.
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.
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.
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%>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With