aspose file tools*
The moose likes Servlets and the fly likes Java.util.Properties Not Found During loading in Servlet/JSP Erro Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Java.util.Properties Not Found During loading in Servlet/JSP Erro" Watch "Java.util.Properties Not Found During loading in Servlet/JSP Erro" New topic
Author

Java.util.Properties Not Found During loading in Servlet/JSP Erro

praveen sharma
Ranch Hand

Joined: May 27, 2006
Posts: 44
hi

i need your help .actually i wanna use property file which contains certain information such as database name/user name/host name/Password .

when i tried to use this file in servlet and Java Server Pages(jsp) i get error - FileNotFoundException /plus native method error i placed the property file(db.properties) into WEB-INF,classes folder and also in respect to WEB-INF folder to but i could not access the file in servlet or Jsp.

if anyone can help me please it will be a great help ......

Thanks
Praveen
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8764
    
    5
let's move this over to the servlets forum


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

How are you trying to read the 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
Based on the most common errors when reading data files from servlets, I would bet that the problem is trying to read a file without specifying a complete path and file name. Any code that depends on the "current" directory will fail in a servlet because the servlet container has its own idea of what the current directory is.

Bill


Java Resources at www.wbrogden.com
praveen sharma
Ranch Hand

Joined: May 27, 2006
Posts: 44
hi friends

well this is my sample code i was using and getting error


and the error which i get after executing above code is as following


Please Help me in order to Solve my problem

waiting for your reply..........Please....


Praveen Sharma
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12678
    
    5


FileInputStream does NOT KNOW that you want this file relative to the servlet context. Look at the getRealPath method of the javax.servlet.ServletContext interface.

Bill
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by William Brogden:


FileInputStream does NOT KNOW that you want this file relative to the servlet context. Look at the getRealPath method of the javax.servlet.ServletContext interface.

Bill


Or, better, look at getResourceAsStream from the same class.
This will return the stream whether you've deployed your app as an expanded file system or from a war fils. The getRealPath method will return null if the app has been deployed as a war file.
Amit M Tank
Ranch Hand

Joined: Mar 28, 2004
Posts: 257
Try the



Do NOT put /WEB-INF/classes

Let me know if it works


Amit Tank
Linked In
praveen sharma
Ranch Hand

Joined: May 27, 2006
Posts: 44
hai friends

thanks for everyone for replying .but i am failed to access the property file .if possible send me dummy sample code so that i can understand more properly..



Thanks
Praveen
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Hi Praveen,

There is a good example in Jaikiram Pai's blog.(He is one of the bartender's in Javaranch)

http://jaitechwriteups.blogspot.com/2007/01/how-to-read-properties-file-in-web.html

Hope this helps,


Rahul Bhattacharjee
LinkedIn - Blog
praveen sharma
Ranch Hand

Joined: May 27, 2006
Posts: 44
good morning everyone

i really thank all of you for helping me .now i am able to access property file in Servlet/Jsp with the help of you guys.

Procedure i used as as follows

my property file is corresponding to WEB-INF folder.



I HOPE now other people can use same funda which i applied.

Thanks alot everyone
Praveen Sharma
SCJP 1.4
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
You could have replaced

Properties p =System.getProperties();
p.load(fp);

with

Properties p = new Properties();
p.load(fp);
praveen sharma
Ranch Hand

Joined: May 27, 2006
Posts: 44
i think both of them will work.....that can be alternative
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by praveen sharma:
i think both of them will work.....that can be alternative


True , both will work.But the purpose of

Properties p =System.getProperties(); is different.Check the javadoc for System for more detail information.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

It is also worth mentioning that getRealPath and anything that refers to Files will fail if it is deployed as a WAR. Given this, it is usually better to work with URLs and Streams rather than Files as URLs and InputStreams will still work with the WAR deployment.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java.util.Properties Not Found During loading in Servlet/JSP Erro
 
Similar Threads
The value for the useBean class attribute StringBean is invalid
making use of servlet through WEB-INF
issue with web.xml
Java.util.Properties Not Found During loading in Servlet/JSP Erro
bean compilation problem