Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JSP UseBean. How to pass PATH-info.  RSS feed

 
Erik Lindholm
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • 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 !!!
 
Marcus Green
arch rival
Rancher
Posts: 2813
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!