• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reading from a system Properties file

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
In my JSp, I am currently trying to read two name:value pairs from a properties file which is placed outside the context of the war and it works fine. My code is as follows
-------------------------------------
<%@ page import="java.io.*, java.sql.*, java.util.*" %>
<%
Properties defaultProps = new Properties();
try {
defaultProps.load(new FileInputStream("C:/server.properties"));
String appserv = defaultProps.getProperty("appserver");
String webserv = defaultProps.getProperty("webserver");
%>
-------------------------------------------
But we DO NOT want to specify a hardcoded location for the server.propertied file as in C:/ etc.
What do I need to do to access my file just as--
defaultProps.load(new FileInputStream("server.properties"));
so that it can be more flexible?
or is there a better way to do this? My server.properties file looks like this
webserver=abc
appserver=xyz

Thankyou.
V.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming that you want the path to be relative to the webapp, you need to do something like this:
ServletContext ctx = getServletContext();
String path = ctx.getRealPath("/");
That will give you the path of the webapp directory, which you can then use as the starting point for a File object. The only catch is that this doesn't work with an unexpanded WAR file. getRealPath will fail if you're running from an unexpanded WAR.
 
Veni Velkoor
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the message, but the thing is that I don't want it relative to the webapp at all.
During the server startup, we will be specifying the location of the file as follws --
java -classpath $CLASSPATH -Dcl.prop.file=
/opt/home/bea/cl.properties ...

What we need is a way to somehow pick up this server.properties file from where it is.
Thanks
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe I am missing something here, but using -D on the java command sets a system property. So given that you have -Dcl.prop.file being set with the actual location of the properties file that you want to load, you can just code the following:

Another way to handle this instead of using -D on the java command line would be to use either a servlet context init parm or a servlet init parm.
I hope this helps,
Mark
[ April 09, 2003: Message edited by: Mark Bensing ]
 
Story like this gets better after being told a few times. Or maybe it's just a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic