Re: FileInputStream in Servlet




<bauerg@xxxxxxx> wrote in message
news:1135964569.250738.326490@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> 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


.



Relevant Pages

  • Re: Tomcat + java.io.FileNotFoundException
    ... Application Server executes servlet and JSP in their own context (both ... > experience configuring tomcat. ... > 1- the File class uses paths relative to the working directory. ... However, classA is ...
    (comp.lang.java.programmer)
  • Re: A2LL ist tot...
    ... | mit Tomcat als Servlet Container. ... | # Bugs - You think you have a bug or there is a difference in behavior with another servlet container. ... | # Database - Getting tomcat to talk to a database. ...
    (de.talk.tagesgeschehen)
  • Re: tomcat error..
    ... Remember that Tomcat doesn't execute JSPs, so it has no way of knowing where in "YOUR FILE" the error is. ... The IDE itself tracks how the code transmutes from source to translated source to object to run-time. ... Use java.util.logging or log4j, and think about the hapless operations folk when you design logging. ... well, the weird thing is, sometimes it shows in what line in JSP servlet the error is, I find my way to the servlet and find line causing error, which is quite useful, but sometimes it doesn't tell you anything, like ...
    (comp.lang.java.help)
  • Re: servlet problems with session attributes
    ... I use session attributes to to preserve data across the ... >> session and to comunicate across servlet objects. ... >> restarting the Tomcat container! ... > one or two jsps and one or two servlets. ...
    (comp.lang.java.programmer)
  • wp-02-0008: Apache Tomcat Cross Site Scripting
    ... Westpoint Security Advisory ... Implementation for the Java Servlet and JavaServer Pages technologies. ... Tomcat has a couple of Cross Site Scripting vulnerabilities. ... possible to cause Tomcat to throw an exception, allowing XSS attacks: ...
    (Bugtraq)