Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 9034
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
let's move this over to the servlets forum
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are you trying to read the file?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think both of them will work.....that can be alternative
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
What is that? Is that a mongol hoarde? Can we fend them off with this tiny ad?
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic