You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Irv Thomae <ir...@succinct.com> on 2001/07/17 04:39:03 UTC

Not finding static files (Tomcat 3.2.2. on RH7)

 My servlet cannot seem to find static text files, no matter where I put them.
This "feels like" a pretty trivial problem - and I feel pretty dumb asking - 
but that's my only option, after ten hours of fruitless experiments, 
alternating with archive-searches of this list, comp.java.lang.*, Sun's 
"Developer Connection", and so on.  
  I'm new to servlets but not to Java.


  Following examples from Jason Hunter's "Java Servlet Programming" (O'Reilly, 
2d ed.), I had a server file structure like this:

  [Tomcat root]
              |webapps
                      |ROOT
                           |WEB-INF
                                  |web.xml
                                  |classes
                                      [assorted 'whatever.class' files]

(Purists note: throughout this post, i will write "[Tomcat root]" as a 
short-cut for the actual path    "/usr/local/jakarta-tomcat-3.2.2"  .)

If I understand Hunter correctly, "/ROOT" is the virtual equivalent of a 
file-system root - so if my servlet code refers to a file as 
"/images/mypix.gif", Tomcat will expect to find that file on my system at
   [Tomcat root]/webapps/ROOT/images/mypix.gif  - is that correct?

Accordingly, I added two more directories off ROOT:

    ROOT
       |html  <--- contains text file "fragment.txt"
       |images
       |WEB-INF<-- contains "web.xml"
             
  
But, the following code (which is supposed to read a fragment of HTML 
"boilerplate" from a file) consistently throws a FileNotFoundException:

    System.setProperty("user.dir","[tomcat home]/webapps/ROOT);
    System.setProperty("user.home","/");

    String wantedFile = "html/fragment.txt");
    FileInputStream fis = new FileInputStream(wantedFile);

Even when a copy of t"fragment.txt" is placed directly in ROOT, and the 
namestring "wantedFile" is changed to just "fragment.txt", the FileInputStr 
constructor still throws a FileNotFoundException.

The story gets more baffling.  To try to understand what was happening, I 
inserted these lines between the last 2 above:

     ServletConfig sConfig = getServletConfig();
     ServletContext ctext = sConfig.getServletContext();
     String realPath = ctext.getRealPath(wanted);

     out.println("Attempting to open '" + wanted "' as " + realPath);

 [where, of course, "out" is a previously-obtained PrintWriter object.)

  That diagnostic causes the browser to display the text
    Attempting to open 'fragment.txt' as [tomcat root]/ROOT/html/fragment.txt

  which is then folowed by the FileNotFoundException report that
     "html/fragment.txt" wasn't found - even though it's there!!!

 Am I misunderstanding the mening of getRealPath() ??
 If not, why isn't the file - which really-truly *is* there - being Found?
 
 Humble thanks in advance for any and all enlightenment....

  Irv Thomae