aspose file tools*
The moose likes Servlets and the fly likes Unable to access config files from a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Unable to access config files from a servlet" Watch "Unable to access config files from a servlet" New topic
Author

Unable to access config files from a servlet

Nadda Habba
Greenhorn

Joined: Feb 16, 2005
Posts: 18
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

Joined: Dec 11, 2004
Posts: 13410

Show us the code you're using to read the config file.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
William Brogden
Author and all-around good cowpoke
Rancher

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


Java Resources at www.wbrogden.com
balakrishnan periysawamy
Greenhorn

Joined: Jun 04, 2007
Posts: 4
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,


BSC(Maths) , MCA , SCJP(1.4) 90%, SCWCD(1.4) 86%, SCBCD(1.3) 92%,SCDJWS 72%, OCA 10g 70%
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unable to access config files from a servlet
 
Similar Threads
Properties file in EJB Ear
read properties file from web.xml or through java
Servlet config in servlets
getResourceAsStream()
Accessing files that are there in classes directory