• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Read Text File

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Awesome. That works perfectly. Thanks, everyone!
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, my bad. You're quite correct.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic