aspose file tools*
The moose likes Servlets and the fly likes How to include a text file in the Javaservlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to include a text file in the Javaservlet?" Watch "How to include a text file in the Javaservlet?" New topic
Author

How to include a text file in the Javaservlet?

Eric Wang
Greenhorn

Joined: Dec 21, 2000
Posts: 22
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

Joined: Mar 22, 2000
Posts: 12835
    
    5
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

Joined: Dec 21, 2000
Posts: 22
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

Joined: Jun 02, 2000
Posts: 58
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

Joined: Jan 01, 2001
Posts: 7
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

Joined: Jan 07, 1999
Posts: 6920
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.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Eric,
Go for .properties method. A sample MIS.properties file which I use is
regds
maha anna
Eric Wang
Greenhorn

Joined: Dec 21, 2000
Posts: 22
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to include a text file in the Javaservlet?