• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can I read property files under tomcat

 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello all
I have simple class that need to read conf file that I have stored into property file
when I try to read from it it says it can't locat it , even so I put it under WEB-INF
how can I read conf files into my class ?
 
Jaikiran Pai
Marshal
Pie
Posts: 10444
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's say you have a war file name myApp.war which contains a conf folder inside which you have place your myprop.properties files. This is how you will access that file from a class in you myApp application:

 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello and tnx for the fast reply
my case is that im not under any servlet but im in simple class that is under tomcat
so the way with getServletContext().getResourceAsStream don�t work for me
another question is , this class finnlly will be reused and not sitt always under tomcat
is there any way to check if im under tomcat or not progremmtclly
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose you could always configure your class to look for property files in a directory with an absolute path. In a servlet use the web.xml to pass the absolute path - I do this all the time.

Bill
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you give me William Brogden short code example?
thanks
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an example app on my site that shows how to set servlet and/or context init params.

http://simple.souther.us
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thenks ben the examples are great and simple
but when i try to run them throw eclipse im geting errors with the
web xml like (on <servlet-class>):

cvc-complex-type.2.4.a: Invalid content was found starting with element 'servlet-class'. One of
'{"http://java.sun.com/xml/ns/j2ee":description, "http://java.sun.com/xml/ns/j2ee":display-name,
"http://java.sun.com/xml/ns/j2ee":icon, "http://java.sun.com/xml/ns/j2ee":servlet-name}' is
expected.


and in servlet-name



cvc-complex-type.2.4.a: Invalid content was found starting with element 'servlet-name'. One of
'{"http://java.sun.com/xml/ns/j2ee":description, "http://java.sun.com/xml/ns/j2ee":display-name,
"http://java.sun.com/xml/ns/j2ee":icon, "http://java.sun.com/xml/ns/j2ee":distributable, "http:
//java.sun.com/xml/ns/j2ee":context-param, "http://java.sun.com/xml/ns/j2ee":filter, "http://java.
sun.com/xml/ns/j2ee":filter-mapping, "http://java.sun.com/xml/ns/j2ee":listener, "http://java.sun.
com/xml/ns/j2ee":servlet, "http://java.sun.com/xml/ns/j2ee":servlet-mapping, "http://java.sun.
com/xml/ns/j2ee":session-config, "http://java.sun.com/xml/ns/j2ee":mime-mapping, "http://java.
sun.com/xml/ns/j2ee":welcome-file-list, "http://java.sun.com/xml/ns/j2ee":error-page, "http://java.
sun.com/xml/ns/j2ee":jsp-config, "http://java.sun.com/xml/ns/j2ee":security-constraint, "http:
//java.sun.com/xml/ns/j2ee":login-config, "http://java.sun.com/xml/ns/j2ee":security-role, "http:
//java.sun.com/xml/ns/j2ee":env-entry, "http://java.sun.com/xml/ns/j2ee":ejb-ref, "http://java.
sun.com/xml/ns/j2ee":ejb-local-ref, "http://java.sun.com/xml/ns/j2ee":service-ref, "http://java.
sun.com/xml/ns/j2ee":resource-ref, "http://java.sun.com/xml/ns/j2ee":resource-env-ref, "http:
//java.sun.com/xml/ns/j2ee":message-destination-ref, "http://java.sun.com/xml/ns/j2ee":message-
destination, "http://java.sun.com/xml/ns/j2ee":locale-encoding-mapping-list}' is expected
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The format for web.xml is pretty picky about the content and order of elements, it looks like that is what it is objecting to. This could be as simple as a missing ">" or "/>" so re-edit your web.xml carefully.

Bill
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try running them in a standalone instance of Tomcat.
These were tested in 5.0 and 5.5.
If you're using an older version, it may be chocking on the servlet spec 2.4 heading.

Or, just look at the code, it's pretty straight forward.
As the name suggests, it's a very simple example of setting and reading init params.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic