Re: Tomcat + java.io.FileNotFoundException

From: Alex Kizub (akizub_at_yahoo.com)
Date: 09/14/04


Date: Tue, 14 Sep 2004 01:30:11 GMT

Application Server executes servlet and JSP in their own context (both
different by the way).
And patrh /xml/abc/xyz is added to this context root.
It is better then read files not from file system but as resources like
this:
 public String stringFromResource(String name) {

  String result = "";
  try {
   URL url = getClass().getResource(name);
   URLConnection conn = url.openConnection();
   InputStream istream = conn.getInputStream();

   BufferedReader in = new BufferedReader(new
InputStreamReader(istream));
   String temp = "";
   while ((temp = in.readLine()) != null)
    result += temp + "\n";
   in.close();
   //System.out.println(result);
  } catch (Exception e) {
   System.out.println(e);
  }
  return result;
 }

At least you know where is your resource root (exactly where is root for
class loading).

But if it's confusing for you I suggest to understand where are you in
file system and then put your files to this place.
Use regular java.io.File and run it INSIDE container! So you can
understand where you are and read files later:

java.io.File file=new java.io.File(".");
or
java.io.File fileRoot=new java.io.File("/");
and then read files in your location and try to understand where it is
eaxctly:

out.println(fileRoor.getAbsolutePath() +" "+fileRoot.getCanonicalPath());

or even this way:
String [] list=fileRoot.list();
if (list!=null) for (int i=0;i<list.length;i++) out println(list[i]);

Alex Kizub.

apatruduque wrote:

> Hello everybody,
>
> I have a question about how to access a file from a .jsp. In summary:
> fileA.jsp uses classA in /WEB-INF/classes to read /xml/fileB.xml (all
> paths relative to tomcat) classA uses the File class to access
> fileB.xml. To put you into context, I´m new to java but have some
> experience configuring tomcat. Environment: Debian Linux with tomcat
> 4.0 and jre_1.4.2
>
> The problem is that I'm constantly getting filenotfoundException.
> After googling for a while I found out the following:
>
> 1- the File class uses paths relative to the working directory. I
> temporarily solved it by using the absolute path /var/lib/.... But
> this is not a solution since I have to deploy the application in
> different OS´s (Linux, Windows and Mac) and I´m not changing the path
> every time I deploy.
>
> 2- Reading the Tomcat FAQ I found out that to read a file I must use
> the ServletContext.getResourceAsStream() method. However, classA is
> not a Servlet...
>
> 3- I made classA derived from HttpServlet and I used the
> ServletContext().getRealPath("/xml/fileB.xml") to obtain the path and
> this path to open the file. I also tryed getResourceAsStream() with
> success.
>
> My question is: How can I access fileB.xml from classA without
> deriving it from the servlet class? Am I making something wrong with
> the design?
>
> Thanks a lot in advance,
>
> Alfonso

apatruduque wrote:

> Hello everybody,
>
> I have a question about how to access a file from a .jsp. In summary:
> fileA.jsp uses classA in /WEB-INF/classes to read /xml/fileB.xml (all
> paths relative to tomcat) classA uses the File class to access
> fileB.xml. To put you into context, I´m new to java but have some
> experience configuring tomcat. Environment: Debian Linux with tomcat
> 4.0 and jre_1.4.2
>
> The problem is that I'm constantly getting filenotfoundException.
> After googling for a while I found out the following:
>
> 1- the File class uses paths relative to the working directory. I
> temporarily solved it by using the absolute path /var/lib/.... But
> this is not a solution since I have to deploy the application in
> different OS´s (Linux, Windows and Mac) and I´m not changing the path
> every time I deploy.
>
> 2- Reading the Tomcat FAQ I found out that to read a file I must use
> the ServletContext.getResourceAsStream() method. However, classA is
> not a Servlet...
>
> 3- I made classA derived from HttpServlet and I used the
> ServletContext().getRealPath("/xml/fileB.xml") to obtain the path and
> this path to open the file. I also tryed getResourceAsStream() with
> success.
>
> My question is: How can I access fileB.xml from classA without
> deriving it from the servlet class? Am I making something wrong with
> the design?
>
> Thanks a lot in advance,
>
> Alfonso



Relevant Pages

  • Tomcat + java.io.FileNotFoundException
    ... fileA.jsp uses classA in /WEB-INF/classes to read /xml/fileB.xml (all ... experience configuring tomcat. ... 1- the File class uses paths relative to the working directory. ... deriving it from the servlet class? ...
    (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)