• 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

How can I read property files under tomcat

 
Ranch Hand
Posts: 620
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
ben josh
Ranch Hand
Posts: 620
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
ben josh
Ranch Hand
Posts: 620
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you give me William Brogden short code example?
thanks
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
ben josh
Ranch Hand
Posts: 620
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Villains always have antidotes. They're funny that way. Here's an antidote disguised as 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