This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading from a system Properties file  RSS feed

 
Veni Velkoor
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • 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.
 
Gary McGath
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Mark Bensing
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!