Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Read Text File  RSS feed

 
Ben Johnson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pretty straightforward... just wanting to read the contents of a text file and print those contents to the browser. When I compile and run my servlet, nothing obvious happens (I just see a blank window). Even if the file "text.txt" doesn't exist, no exception is thrown, so I can't really figure out what I'm doing wrong.

Any help is very much appreciated. I know there is an I/O/Streams forum, but this seems like too much of a beginner question to go there.



Thanks much!

Ben
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out goes to the standard output of the Servlet container -- probably into a log file somewhere. You need to print to the response writer:

PrintWriter out = response.getWriter();

Then use this "out" instead of System.out.

We have a forum devoted to servlets here. I will move this thread there for any further discussion.
 
Ben Johnson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Ernest. That solved the problem. I do have a couple of other questions, though.

1.) Did I put the line you suggested in the right place? When I added that line, my attempt to compile failed, with "unreported exception java.io.IOException; must be caught or declared to be thrown" and was referencing the .getWriter() method. I'm still trying to learn about throwables and exceptions, in general, but adding "throws IOException" to doGet fixed the problem. I don't like fixing things without knowing why...

2.) Can the location of the text file be relative? I was using "text.txt", but when I added the "throws", I was able to see in the output window (my cmd.exe that's running Tomcat) that Java couldn't find the file. Making it an absolute location, as shown below, works, but that obviously limits portability.

 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. the doXXX() methods of the servlets are generally declared as throwing IOException() and ServletException(). These are perfectly normal. If you try to throw a different kind of exception, one that doesn't extend these two, you'll get a compile error.

2. If you use Java I/O, the relative location will be from wherever the server is executing from, "C:\jakarta-tomcat-5.5.4\bin" in your case. What you want to do is use the ServletContext getResourceAsStream() method. The absolute path using that will be from your web application root, and won't even be allowed to access anything above that.

So in your case:



-Yuriy
[ August 18, 2005: Message edited by: Yuriy Zilbergleyt ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the getRealPath() or getResourceAsStream() methods of the servlet context.
 
Ben Johnson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome. That works perfectly. Thanks, everyone!
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to provide another option, you can use the RequestDispatcher to return other resources as well. If you place test.txt in the WEB-INF directory you can do this:

and remove everything else
 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, I never realized that, that's pretty cool. But if the text file is in the WEB-INF directory, wouldn't that be getRequestDispatcher("/WEB-INF/test.txt")?
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, my bad. You're quite correct.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!