File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes How do you read a file in Servlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How do you read a file in Servlet?" Watch "How do you read a file in Servlet?" New topic
Author

How do you read a file in Servlet?

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I want to get the contents of the file (which is a part of the HTML file) and redirect to the brower.
ie
out.println("<html>");
out.println("<body>");
:
:
//here read the contents of the file.html, which contains a html //table, redirect that content to the browser
:
:
out.println("</body>");
out.println("</html>");

I tried,
response.sendRedirect("c:\file.html");
It is just displaying the contents of file but not the other html tags which I have written before and after I read the file.
How to do this?
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
You should simply read the file and run out.println for each record read.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
In that case I did try this code, but I am getting. Its not printing the output at all. But when I compile and run the try/catch block code in a seperate java file, it is printing the contents to the console.
out.println("<html>");
out.println("<body>");
:
:
try
{
String parent = null;
File file = new File(parent, "con1.txt");
FileReader fileReader = new FileReader(file);
BufferedReader buffReader = new BufferedReader(fileReader);
String buffer = new String();
while( (buffer = buffReader.readLine() ) != null)
System.out.println(buffer);
buffReader.close();
}
catch(IOException e)
{
}
:
:
out.println("</body>");
out.println("</html>");
Please tell me how should I direct the ouput the browser.
Thanks

Contents of con.txt is
<table cellpadding="2" cellspacing="0" align="center" width="750" border="1">
<tr align="center"><TD><STRONG><FONT size="2">ID</FONT></STRONG></TD>
<TD><STRONG><FONT size="2">SKU_Number</FONT></STRONG></TD>
:
:
:
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
You are sending the results of the read out to System.out.println. You want to send them to the same place that you are sending the other HTML, that is, out.println(buffer);
Give that a try:


[This message has been edited by Thomas Paul (edited September 28, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I am sorry, that was a typing mistake.
I did try with
out.println(..)
But it's not working.
But when I try compiling in a separate java file (not servlet)
it works. What might be wrong?
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
I think you have to ESCAPE your double quotes. You have to put a 'backslash', before the double quotes .
To confirm this I wrote a small jsp and checked. For servlet also it should be fine , since the 'out' variable represents the HttpServletResponse type object.
// This code WORKS.

I saw the browser displaying the table with 1 row and 2 cols(ID,SKU_Number). Try this in the java code also. You may have to write a method which takes the buffer (String) as input and returns a String with the 'doublequotes' escaped. Then you do out.println(convertedString);
regds
maha anna
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
No, you would only have to do that if the text was in the Java program. Since the text is in a file, you don't need to escape the quotes. I used that program exactly as written and it worked perfectly. Are you sure that the con1.txt file is where it is supposed to be? Try specifying the exact directory. Also, put in a default error message in case the IOException is thrown. Write now you can't tell if it is failing to find your file or if it is reading it and not producing any results.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thank you so much Thomas Paul. You are right. I was not giving the path of the directory correctly. Such a bummer I am.
I really appriciate all the help!
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
By the way, Jairat, the people who run this little saloon would prefer if you use your full name (first and last) rather than just Jairat. Thanks!
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
I don't know how you worked out the correct file location in the end, but the "official" way in a servlet is to use:
getServletContext().getRealPath("filename");
This is important with some Servlet Containers, which don't set the "current directory" for file access to be anything reasonable.


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

Joined: Nov 22, 2008
Posts: 18944
My file 'conten1.html' is stored under
c:\javawebserver2.0\servlets\content1.html
But when in MyServlet.java (which again is stored under the same dir) when I use,
getServletContext().getRealPath("conten1.html");
I expected it to return the above pat, but I am surprised when the return path was
c:\javawebserver2.0\public_html\content1.html
I have no clue how this 'public_html' folder which is under
c:\javaweserver2.0 is getting into picture.

Thomas Paul, my full name is Jaishankar Rathnakaran, I prefere to use the srort form JaiRat.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do you read a file in Servlet?