This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Tomcat and the fly likes Unable to read properties file from Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Unable to read properties file from Servlet" Watch "Unable to read properties file from Servlet" New topic

Unable to read properties file from Servlet

Srinivas Kumar
Ranch Hand

Joined: Jul 14, 2005
Posts: 52
I have some problem in reading the properties file from servlet.I'm using apache tomcat server. Following is the code snippet.I tried putting the properties file in all possible places(classes, webApps, in the context path) but the application failed to load it.It is giving nullpointer exception at prop.load(is);
public class LoadProps {

public Properties getProps() {
String propertyfile = "";
Properties props = new Properties();

try {
is = this.getClass().getResourceAsStream(propertyfile);
}catch (Exception ioe) {
return props;

Appreciate your help
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

This faq entry shows two ways to do it:

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Pedro Erencia
Ranch Hand

Joined: Apr 03, 2008
Posts: 70
Take a look to ServletContext.getResourceAsStream.
If your properties file is located in tomcat-dir/webapps/myapp/WEB-INF/ you could use getServletContext().getResourceAsStream("/WEB-INF/").
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63540

"VSSM Kumar", you have previously been warned on one or more occasions regarding adjusting your display name to meet JavaRanch standards. This is not optional. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it prior to your next post.

Your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Be aware that accounts with invalid display names are disabled.

JavaRanch Sheriff

[Asking smart questions] [About Bear] [Books by Bear]
Srinivas Kumar
Ranch Hand

Joined: Jul 14, 2005
Posts: 52
I found some strange things.The servlet class file (not source file) is there in my workspace (c:\workspace\<myappName>\build\classes)and nowhere tomcat server is totally in differnt path.The serlvet mapping in web.xml has the url pattern "/<somepath>".How come at runtime my application could find the servlet class without copying the servlet class file on to the server (i mean deployment)? I'm using Eclipse as IDE.Does Elipse do it automatically? If so, where does it place the class file? I searched my entire C: drive for the class file but could not find it.
I agree. Here's the link:
subject: Unable to read properties file from Servlet
jQuery in Action, 3rd edition