aspose file tools*
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
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Unable to read properties file from Servlet" Watch "Unable to read properties file from Servlet" New topic
Author

Unable to read properties file from Servlet

Srinivas Kumar
Ranch Hand

Joined: Jul 14, 2005
Posts: 52
Hi,
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 = "myProps.properties";
Properties props = new Properties();

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

}
Appreciate your help
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

This faq entry shows two ways to do it:
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
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/file.properties you could use getServletContext().getResourceAsStream("/WEB-INF/file.properties").
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61412
    
  67

"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.

bear
JavaRanch Sheriff


[Asking smart questions] [Bear's FrontMan] [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 else.my 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: http://aspose.com/file-tools
 
subject: Unable to read properties file from Servlet