• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unable to access config files from a servlet

 
Nadda Habba
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have kept my logger configuration file[config.properties] at /WEB-INF/config folder.But iam unable to read the file from my servlet class. In my servlet iam trying to read the file using path '/WEB-INF/config/config.properties'. Have i placed the file at the right location ? Would it be included in the classpath? Thanks in advance.
Nadda
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Show us the code you're using to read the config file.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would it be included in the classpath?

If you mean the CLASSPATH environment setting, servlet containers generally ignore it. Servlet containers also ignore the "current" directory.

Your problem is probably related to the difference between the way the server "sees" application relative paths and the way the operating system sees absolute paths. If you are trying to read a file using typical java.io.File related methods, you need absolute paths.

Bill
 
balakrishnan periysawamy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nadda,
Normally when you use ResourceBundle or Properties file, server doent look inside the classes folder.

To access any file you need to get the entire path of the file.

For eg:-
If you have created your project inside d:/project/pro1

Then the entire path is d:/project/proc1/WEB-INF/config/config.properties


To do that,
ServletContext context = servletConfig.getServletContext();
String strPath = context.getRealPath("/WEB-INF/config/config.properties");

You can pass this strPath to load your configuration file.

Regards,
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by balakrishnan periysawamy:
Normally when you use ResourceBundle or Properties file, server doent look inside the classes folder.

To access any file you need to get the entire path of the file.


This is misleading. Firstly, it depends on whether you want your reference as a File, URL or Stream. If you decide to package code as a library, it makes sense to use the ClassLoader to find resources attached to your code.

If you package as a web application, you may decide to refer to resources relative to the context root, but from experience I prefer to refer to web resources by URL or Stream, since you cannot get an absloute File reference when it is packaged in a WAR file.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic