Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Curious problem in reading properties file in Tomcat  RSS feed

 
Sajee Joseph
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I have a certain problem in reading a properties file on my Tomcat 4.1 server.
The code to read the properties file is in a servlet.
Currently the code to read the Properties file is:
FileInputStream iStr = new FileInputStream("../webapps/gem/WEB-INF/gem.props")
Please note that the servlet is present in the 'gem' folder.
The above code works fine. But i noticed that on some machines where Tomcat is started in a different manner, it gives an exception saying "Unable to read ../webapps/gem/WEB-INF/gem.props".
For instance i have a Tomcar Start up url on my Desktop with target as 'D:\j2sdk1.4.0\bin\java.exe -jar -Duser.dir="D:\Program Files\Apache Group\Tomcat 4.1" "D:\Program Files\Apache Group\Tomcat 4.1\bin\bootstrap.jar" start'.
With this i am able to startup the server But i get the above mentioned exception.
Is there a means to uniformly read a Properties file irrespective of how Tomcat is started.
Guys you really gotta help me on this. Plzz

Regards,
Saj
 
Praful Thakare
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sajee,

If its just about reading the properties files then following code may help you.




Cheers
-Praful
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem here is "what does the current directory" mean. Basically the default directory is where ever you run the application from. However if you start Tomcat using the batch file in the tomcat directory or using your script, the current directory value changes.

As Praful Thakare hinted, it probably isn't the best having hard-coded values in your applications which depend on where the application was started from. I'm not sure his solution was quite right though.

One solution in web applications is to use the javax.servlet.ServletContext getResource or getResourceAsStream methods. Check the API for usage.

The reason I'm not sure of the previous code is that the WEB-INF/classes directory is on the classpath seen by that ClassLoader, but the WEB-INF directory is not. You can place your file in the classes directory (a common solution) or use the ServletContext as mentioned.

Dave
 
Praful Thakare
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sajee,

As mention by David u need to put gem.properties in WEB-INF/classes for my code to work.
As Praful Thakare hinted, it probably isn't the best having hard-coded values in your applications which depend on where the application was started from. I'm not sure his solution was quite right though.


David,thanx a bunch for comments but wondering whats hard coded in the above code other then the conf file name,also what are the possible drawbacks of this approch ?

Cheers
-Praful
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was refering to this in the original post: new FileInputStream("../webapps/gem/WEB-INF/gem.props") since it relies on where the application is run from and doesn't work if it is packaged as a WAR.

getResource and getResourceAsStream from ServletContext and ClassLoader both use URLs and InputStreams so they will still work with WARs and don't care where the context is placed or where the app server is run from.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The third item on this list shows exactly how to load a properties file from within a webapp in Tomcat.

http://wiki.apache.org/jakarta-tomcat/HowTo
 
Praful Thakare
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was a good input..thanx a bunch

-Praful
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!