File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
praveen sharma
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8898
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
let's move this over to the servlets forum
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you trying to read the file?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13044
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
praveen sharma
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13044
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 257
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try the



Do NOT put /WEB-INF/classes

Let me know if it works
 
praveen sharma
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2308
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
praveen sharma
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2308
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could have replaced

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

with

Properties p = new Properties();
p.load(fp);
 
praveen sharma
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think both of them will work.....that can be alternative
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic