• Post Reply Bookmark Topic Watch Topic
  • New Topic

FileReader - Servlet Problem.....Cant find file  RSS feed

 
DC Dalton
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just built a servlet that pulls the page content from a .txt file (to eliminate db strain) I have it working fine on the local machine but when I test it on a server I keep getting cant find file xxx.txt. It doesnt throw any exceptions & the page loads the top/bottom header but no text. I have heard this can be a real pain but no one seems to have a way to fix it! The server is a linux on apache using resin as the servlet engine. I have tryed the full url of the .txt file as well as the unix path usr/local/public_html/articles/testText.txt. I also put the text file into the classes folder figuring if it was in the same folder as the servlets it wouldnt have a problem.........NO SUCH LUCK. Anyways here is the code, if anyone can help with this I would surely appreciate it.
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class pageServTest extends HttpServlet {
public void init(ServeltConfig config) throws ServletException {
super.config(config);
}
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException {
try {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String pageName = "Test Page";
HtmlTop top = new HtmlTop();
HtmlBot bot = new HtmlBot();
top.DrawTop(out, pageName, 1);
FileReader fr = newFileReader("this is the path I cant figure out");
BufferedReader br = new BufferedReader(fr);
String s;
while ((s = br.readLine()) != null) {
out.println(s);
}
fr.close();
bot.DrawBot(out, pageName);
}
catch (IOException ioe) {
System.err.println(ioe.getMessage());
}
}
public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
doPost(req, res);
}
}
As I said before ive tried ever possible path I could think of in the new FileReader(" "); Ive tried http://url/articles/testText.txt, ../../articles/testText.txt & the ones above.

[This message has been edited by DC Dalton (edited August 22, 2001).]
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are several ways to solve this. The approach I would use is to include the file in a directory under my web application. Such as < webapp_root >\articles\testText.txt To get this, you can use the ServletContext object. ServletContext allows access to resources in your web application.


------------------
Hope This Helps
Carl Trusiak, SCJP2
[This message has been edited by Carl Trusiak (edited August 23, 2001).]
[This message has been edited by Carl Trusiak (edited August 23, 2001).]
 
DC Dalton
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks much Carl....that worked like a charm. One last question about doing this. I was speaking to another programmer last night about this & he said he remembered hearing somewhere that pulling text from a flat file like this wasnt reall a good idea. He couldnt remember why they had said it but it was also a site that was using the db for text & they want to to take some of the strain off the db server...Do you know of any real downsides to this type of thing. I look at it in the sense that anything less proccessor/memory intensive has to be good. Also, being the inquizitive mind that I am, I wanted to learn more about this so I went to the servlet API but dont see this method anywhere under ServletContext. Where are they hiding it at? Thanks again!
[This message has been edited by DC Dalton (edited August 23, 2001).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!