• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to include a text file in the Javaservlet?

 
Eric Wang
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, All:
Hope someone can help me this problem? Any reply will be help for me.
regards
Eric
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you tried so far?
The simplest thing is to just open the file and read it into a byte array, then output the byte array.
 
Eric Wang
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi William Brogden:
First thanks for the reply, this is what i want to do in my java program.
I have few java program, they are using the same value for this three parameter.
1. this.driverName
2. this.conString
3. this.sql
this.driverName="oracle.jdbc.driver.OracleDriver";
this.conString="jdbc racle:thin:admin/user@backup..com.sg:1521 rcl";
this.sql="select * from Table1";
So I want to save this three parameter value in a text file, so every time I want to change it, I only need to change the text file, so all the java program will be change.
So how should I retrieve this parameter value from this text file? Or any other ways can slove this problem?
cheers
Eric
 
Raghavendra Holla
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
Keep these string values in a file say "c:\myinput.txt", and open this file using "FileReader" class. Pass this Reader objects to "BuffredReader".
e.g. code is...
BufferedReader bd= new BufferedReder(new FileReader("c:\myinput.txt"));
Now use "readLine" Method of BufferedReader to get the each line.
Still better way is to keep all your input in XML file and use XML parser to get the required tag values. If you have Tomcat as servlet runner, you have to set up all its parameters in XML file only.
Bye.
Regards,
Holla.
 
vikas sahni
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
make properties file instead of text file. this is very simple.
and then in your program retreive like this
Properties pr=new Properties();
String fname=new Properties("yourfile.properties");
FileInputStream in=new FileInputStream(fname);
prop.load(in);
String driver=prop.getProperty("this.driverName");
String con=prop.getProperty("this.conString");
and then u can make use of these strings anywhere like
DriverManager.getConnection(con);
hope this would help.
vikas
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A small correction to the code provided by vikas sahni. I think the extra "Properties" constructor was a typo, the code should probably look more like:

Note that for a properties file you should also remove the " quotes from your data values.
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric,
Go for .properties method. A sample MIS.properties file which I use is
regds
maha anna
 
Eric Wang
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, vikas sahni, Frank Carver, maha anna �CRaghavendra Holla :
Thank you very much for all your replies. I have been slove my problem. Thanks again. You are really a Java Expert. I hope this forum will keep going, and hope more people join in to this forum.
cheers
Eric
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic