File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unable to read properties file from Servlet

 
Srinivas Kumar
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Pedro Erencia
Ranch Hand
Posts: 70
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64204
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
Srinivas Kumar
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic