It's not a secret anymore!*
The moose likes Tomcat and the fly likes How can I read property files under tomcat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "How can I read property files under tomcat " Watch "How can I read property files under tomcat " New topic
Author

How can I read property files under tomcat

Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
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 ?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

See:
http://wiki.apache.org/tomcat/HowTo#head-45c3314139cb900ddd43dde2ff671532e6e844bc


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9929
    
158

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:



[My Blog] [JavaRanch Journal]
Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
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

Joined: Mar 22, 2000
Posts: 12759
    
    5
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

Joined: Apr 27, 2006
Posts: 597
can you give me William Brogden short code example?
thanks
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Apr 27, 2006
Posts: 597
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

Joined: Mar 22, 2000
Posts: 12759
    
    5
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

Joined: Dec 11, 2004
Posts: 13410

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.
 
Don't get me started about those stupid light bulbs.
 
subject: How can I read property files under tomcat
 
Similar Threads
How to configure property files in a War?
Confused (JWS)
how can i put configurations files outside war file
How to configure property files in a War?
Property File inside the jar