7

I've gotten as far as this:

private Properties logoUrls = new Properties();
logoUrls.load(new FileInputStream("channelLogos.properties"));

where channelLogos.properties is in the same directory as my JSP. I get a FileNotFound exception. Where does my app actually think I mean by "channelLogos.properties", if not the same directory as the JSP? How can I determine the correct path to load the properties file?

Robert Munteanu
  • 67,031
  • 36
  • 206
  • 278
morgancodes
  • 25,055
  • 38
  • 135
  • 187
  • related http://stackoverflow.com/questions/3920088/jsp-servlet-read-parameters-from-properties-file – Adriano Nov 29 '13 at 13:47

6 Answers6

11

This will do the job:

<%@page import="java.io.InputStream" %>
<%@page import="java.util.Properties" %>

<%
    InputStream stream = application.getResourceAsStream("/some.properties");
    Properties props = new Properties();
    props.load(stream);
%>

Anyway I really think you should have the properties file in the classpath and use a servlet

victor hugo
  • 35,514
  • 12
  • 68
  • 79
6

I'd like to highly recommend reading about Model 2 Servlets. I recommend it to everyone who's still doing Model 1 Servlets, that is, doing "real work" in a JSP.

As to your question: First, throw the properties file in your classpath, then read the file using getResourceAsSttream:

Thread.currentThread().getContextClassLoader().getResourceAsStream("channelLogos.properties");

There are many options, of course, and everyone will have their favorite.

Don Branson
  • 13,631
  • 10
  • 59
  • 101
4

When you say "same directory as JSP", what exactly do you mean by that? That your JSP sits somewhere in, say, /mywebapp/somefolder/my.jsp with mywebapp being your application root and your properties file is /mywebapp/somefolder/channelLogos.properties?

If so, then most likely they're NOT in the same directory. JSP has been compiled and where it is actually located may vary depending on servlet container. Your best bet is to use ServletContext.getRealPath() as suggested by pkaeding with properties file path relative to webapp context as an argument. Using the above example:

private Properties logoUrls = new Properties();
logoUrls.load(new FileInputStream(servletContext.getRealPath("/somefolder/channelLogos.properties")));

That said, keep in mind that if you insist on putting your properties in the same folder as JSP you should take care to restrict it from being publicly accessible (unless that's the intention).

ChssPly76
  • 99,456
  • 24
  • 206
  • 195
  • Thanks. This works. However, I was planning on doing all of this inside jspInit, which doesn't have easy access to the servletContext. Maybe a classpath approach really is best. – morgancodes Jul 17 '09 at 14:47
  • `getServletConfig().getServletContext()` will get you servletContext from within `jspInit` method. That said, classpath approach is totally better and MVC is better yet :-) I've only provided this answer because you've insisted that you want to do it in JSP alone. – ChssPly76 Jul 17 '09 at 16:13
3

Take a look at ServletContext.getRealPath(). That should give you the full path to the properties file.

pkaeding
  • 36,513
  • 30
  • 103
  • 141
  • That's great to know about. Thanks! Using that technique, it shows me that my path is seemingly correct to my properties file, but I still get a file not foud exception. Any ideas? – morgancodes Jul 16 '09 at 22:52
0

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.

VAYU
  • 29
  • 5
0

In properties file, properties are listed as below:

     label.esme.interface.dependenciesDelete = "Dependencies present. Cannot delete."

In case you are using Struts framework, it can be present in struts-config.xml like:

    <message-resources parameter="pathToApplcnRsrc"/>

or struts.properties like:

    struts.custom.i18n.resources = ApplicationResource

Use a property from properties file like below in JSP:

    '<s:text name="label.esme.interface.dependenciesDelete" />'
Nancy
  • 158
  • 2
  • 11