• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JSP UseBean. How to pass PATH-info.

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello.
I am quite new to JSP and Java.
I am working on an application where I intend to
use the USEBEAN-tag to invoke a JavaBean that
will read an ordinary inputfile (.txt).
My problem is this:
How can I pass a parameter to this Javabean to
tell what PATH this file is in ?
Or is there a way that this JavaBean can find
out that by itself ?
==> is there i.e any way a JavaBean can read
files in an init-method or constructor-
method where the files is kept in an
spesific path ? (I think of the way Servlets
has its config-files. With this kind of
technique the PATH-information could i.e
be a value in a 'config'-parameter.)
hope anyone can help me !
I read Javaranch a lot and find it very useful !!!
 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
InputStream is = getClass().getResourceAsStream("db.properties");
Properties props = new Properties();
String sUrl="";
String sDriver="";
String sUser="";
String sPassword="";
try{
props.load(is);
sUrl=props.getProperty("url");
sDriver=props.getProperty("driver");
sUser = props.getProperty("user");
sPassword=props.getProperty("password");
This will access a file (db.properties in this case) from the same dir as the class resides in. Doesn't tell you the current dir however.
Marcus
 
Erik Lindholm
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.
I tried out this suggestion, and it worked just fine. This helps me a lot.
I want to do a similar retrival of path-information from a Java batch-application (with a MAIN-method). The statement
getClass().getResourceAsStream("db.properties") is not valid in this kind of program. This is the error-info I get:
"Cannot make a static reference to the instance method named getClass for class java.lang.Object with arguments ()"
I guess that I have to initialize ("New...") an object in front of my command.
any suggestions to this ?
I know that this question should not be placed under the JSP-forum, but it is so similar to my first question that I take the chance.
 
Get off me! Here, read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic