FileInputStream in Servlet

B

bauerg

Hi there!
I am not sure to be right here at this forum, but:

I have a servlet, which I want to debug within eclipse.
The servlet is reading a config-file on startup, this file is opend
with a FileInputStream(filename), but without a path!

I have defined the Tomcat4 for MyEclipse with different HOME and BASE,
if I set a fix path (i.e. "c:/file.ext") then everythin works fine, but

without the path, the FileInputStream everytime gives a exception:
java.io.FileNotFoundException: file.ext

I have placed the file.ext in many directories, but it never has been
found!

Where to hell does Tomcat search for this file???

Please could someone help me? Do I have to place the file in
TOMCAT_HOME, TOMCAT_BASE, somwhere under one of this, or what?

thanks in advance
Georg
 
N

none

Hi there!
I am not sure to be right here at this forum, but:

I have a servlet, which I want to debug within eclipse.
The servlet is reading a config-file on startup, this file is opend
with a FileInputStream(filename), but without a path!

I have defined the Tomcat4 for MyEclipse with different HOME and BASE,
if I set a fix path (i.e. "c:/file.ext") then everythin works fine, but

without the path, the FileInputStream everytime gives a exception:
java.io.FileNotFoundException: file.ext

I have placed the file.ext in many directories, but it never has been
found!

Where to hell does Tomcat search for this file???

Please could someone help me? Do I have to place the file in
TOMCAT_HOME, TOMCAT_BASE, somwhere under one of this, or what?

thanks in advance
Georg
have your tried placing the file in the same location as the class that
reads it?

Tim
 
R

Rhino

Hi there!
I am not sure to be right here at this forum, but:
Yes, this is a reasonable place to ask your question.
I have a servlet, which I want to debug within eclipse.
The servlet is reading a config-file on startup, this file is opend
with a FileInputStream(filename), but without a path!

I have defined the Tomcat4 for MyEclipse with different HOME and BASE,
if I set a fix path (i.e. "c:/file.ext") then everythin works fine, but

without the path, the FileInputStream everytime gives a exception:
java.io.FileNotFoundException: file.ext

I have placed the file.ext in many directories, but it never has been
found!

Where to hell does Tomcat search for this file???

Please could someone help me? Do I have to place the file in
TOMCAT_HOME, TOMCAT_BASE, somwhere under one of this, or what?
I haven't written a servlet in a couple of years so my memory is a bit hazy
for where Tomcat4 looked for files. However, I've looked at my first
servlet, which does some file I/O, and my old Tomcat4 setup, and I _think_
your servlet will look for the file in the webapps folder. Within the
webapps folder, you will have a variety of other folders, one of which
should have the same name as your servlet; I think the file you want to be
read should be in that folder.

For example, if your servlet is called Foo, go to your tomcat directory; in
my case, this is d:\jakarta-tomcat-4.1.29. Then enter the webapps directory
within Tomcat. Then enter the Foo directory within the webapps directory. I
_think_ that Tomcat will find your file if you put it there and then simply
ask for it by its simple name, e.g. file.ext.

However, if that does not work, add this code to your servlet (temporarily)
to find out where the servlet wants the file to be:

String realPath =
this.getServletConfig().getServletContext().getRealPath("/");
log ("realPath=" + realPath);

Execute the servlet in Tomcat, or at least enough of it that those two lines
execute. Then, find your log file and see what it has told you about the
value of 'realPath'. I honestly don't remember where Tomcat4 writes the log
any more: I think it is probably catalina.out but it may have been
localhost_log.2005-12-30.txt or one of the other files in the logs directory
of Tomcat. In any case, the value of 'realPath', wherever it is written,
_should_ be the directory where it was looking for the file. Just place the
file in that directory and this time your servlet should find the file.
(Naturally, you can remove the two lines I suggested after executing the
servlet the first time.)

From then on, all references to external resources like files should be made
relative to the value given to you by 'realPath'. Therefore, if realPath is
d:\jakarta-tomcat-4.1.29\webapps\Foo and you want to put the file in
d:\jakarta-tomcat-4.1.29\webapps\Foo\Resources\Files\file.ext, I think you
should be referring to the file as "Resources/files/file.ext"; you should
_not_ refer to it by the absolute path.

For more information about this subject, consult a good servlet programming
book. When I started writing servlets in 2002, several people recommended
Jason Hunter's Java Servlet Programming, Second Edition. I'm not sure if
anything better has come along since so you might want to ask on this
newsgroup to see.

Rhino
 
J

John C. Bollinger

I have a servlet, which I want to debug within eclipse.
The servlet is reading a config-file on startup, this file is opend
with a FileInputStream(filename), but without a path!

I have defined the Tomcat4 for MyEclipse with different HOME and BASE,
if I set a fix path (i.e. "c:/file.ext") then everythin works fine, but

without the path, the FileInputStream everytime gives a exception:
java.io.FileNotFoundException: file.ext

I have placed the file.ext in many directories, but it never has been
found!

Where to hell does Tomcat search for this file???

Please could someone help me? Do I have to place the file in
TOMCAT_HOME, TOMCAT_BASE, somwhere under one of this, or what?

As I recall, Tomcat provides working space on a per-webapp basis
somewhere under its own directory hierarchy. I believe the location can
be configured, in fact. You'll find it in the docs if you read long enough.

But why not make Tomcat tell you instead? Just have a servlet or JSP
(running in your debug environment) send the value of the System's
"user.dir" property; this should name the directory you want.
 
J

Juha Laiho

(e-mail address removed) said:
I have a servlet, which I want to debug within eclipse.
The servlet is reading a config-file on startup, this file is opend
with a FileInputStream(filename), but without a path!

It would read the file from the "current working directory" of the
running Tomcat process. But then, I'm not at all certain whether
there is any specification to say what should be the current working
directory for a servlet container - so even if you find this
information for some version of some specific container (such as
for Tomcat 4.0.28), there's no guarantee that it would remain same
even for the next patch version (on the other hand, there's no
guarantee that the directory would remain constant over the lifetime
of the container). So, please do not use this method (or any which
relies on the "current working directory") when writing code to be
executed within a servlet container.

Rather, use ServletContext.getResourceAsStream(); see the javadoc
AND servlet specification (chapter SRV.3.5; Servlet Context/Resources)
for details. This will work the same on every container that conforms
to the specification (and diverging from the specification would be
considered a bug).

Documentation (the javadoc as well as the servlet specification) is
available from

http://jcp.org/aboutJava/communityprocess/final/jsr154/index.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top