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 Where to save the text file when reading from a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "Where to save the text file when reading from a servlet" Watch "Where to save the text file when reading from a servlet" New topic
Author

Where to save the text file when reading from a servlet

Ullas Madathil
Greenhorn

Joined: Apr 26, 2006
Posts: 2
Hi friends ,
I am devloping a application which requires a servlet to read text file.I have written the code and it si not showing any error .But the problem when i start my Tomcat server then it shows the text file not found error and automaticaaly shutsdown.I have stored the text file in \webapps\ROOT\WEB-INF\classes directory.plz help me
Thanking all in advance
Ullas
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1506
The error is self explanatory. It's just not finding the file. Post the code you are using to access the file.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Ullas Madathil
Greenhorn

Joined: Apr 26, 2006
Posts: 2
public class GetPageServer extends HttpServlet
{
protected void doGet(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException
{
BufferedReader textreader = new BufferedReader(new FileReader(textfile));
FileOutputStream exstufile = new FileOutputStream(new File("STU.txt"));
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Because a servlet app can be deployed from either a file system structure or a war file, there is no guarantee that your file will exist on the systems hard drive as a 'file'.

The ServletContext.getResource and ServletContext.getResourceAsStream methods are provided for just this reason. If you use one of these methods, you will be able to refer to your file, relative to the context root of the app.

http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56209
    
  13

"Ullas V M",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Where to save the text file when reading from a servlet
 
Similar Threads
Problem with packaging and deploying
custom 503 error page thrown from servlet java
Downloading a file from servlet
clobResultSet.next() throws error
Displaying the servlet from the servlet