Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
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 OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 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

How to include a text file in the Javaservlet?

Eric Wang

Joined: Dec 21, 2000
Posts: 22
Hi, All:
Hope someone can help me this problem? Any reply will be help for me.
William Brogden
Author and all-around good cowpoke

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

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.conString="jdbc racle:thin:admin/ 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?
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.
vikas sahni

Joined: Jan 01, 2001
Posts: 7
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("");
FileInputStream in=new FileInputStream(fname);
String driver=prop.getProperty("this.driverName");
String con=prop.getProperty("this.conString");
and then u can make use of these strings anywhere like
hope this would help.
Frank Carver

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 ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
maha anna
Ranch Hand

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

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.
I agree. Here's the link:
subject: How to include a text file in the Javaservlet?
jQuery in Action, 3rd edition