This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do you read a file in Servlet?

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should simply read the file and run out.println for each record read.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic