A
apatruduque
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
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