You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Giuseppe Briotti <g....@mclink.it> on 2004/10/08 15:07:01 UTC
File retrieving
Hi all!
I need to retrieve a file from inside a servlet. If I try:
Url myURL = servletcontext.getResource("/WEB-INF/myFile.xml");
File myFile = myURL.getFile();
If I try this, I obtain a different behaviour depending on
the web container:
on Tomcat 4 myURL contains \localhost\myapp\WEB-INF\myFile.xml i.e.
path relative to the host running application
on Jetty myURL contains c:\mywebserver\myapp... i.e. absolute path
Thus, I think that there is a better method to retrieve the file,
isn't it?
Any ideas or suggestions?
G
---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
Re: File retrieving
Posted by Tim Funk <fu...@joedog.org>.
FAQ http://jakarta.apache.org/tomcat/faq/misc.html#getResourceAsStream
-Tim
Giuseppe Briotti wrote:
> Hi all!
>
> I need to retrieve a file from inside a servlet. If I try:
>
> Url myURL = servletcontext.getResource("/WEB-INF/myFile.xml");
> File myFile = myURL.getFile();
>
> If I try this, I obtain a different behaviour depending on
> the web container:
>
> on Tomcat 4 myURL contains \localhost\myapp\WEB-INF\myFile.xml i.e.
> path relative to the host running application
>
> on Jetty myURL contains c:\mywebserver\myapp... i.e. absolute path
>
> Thus, I think that there is a better method to retrieve the file,
> isn't it?
>
> Any ideas or suggestions?
>
---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
Re: File retrieving
Posted by QM <qm...@brandxdev.net>.
On Fri, Oct 08, 2004 at 03:07:01PM +0200, Giuseppe Briotti wrote:
: I need to retrieve a file from inside a servlet. If I try:
:
: Url myURL = servletcontext.getResource("/WEB-INF/myFile.xml");
: File myFile = myURL.getFile();
:
: If I try this, I obtain a different behaviour depending on
: the web container:
: [snip]
: Thus, I think that there is a better method to retrieve the file,
: isn't it?
:
There are three reliable ways to do this:
1/ put the specified file under the classpath, such as in a JAR or under
WEB-INF/classes, such that you can fetch it using getResourceAsStream().
This is suitable if you don't want the data to be accessible to clients
via GET requests.
2/ Use the RequestDispatcher to forward() to the data (if you want
clients to get just that file) or include() it (if you're putting
several different files' data in the response). This works well if the
file in question is under the document root, that is, it would otherwise
be accessible via a client's GET request.
3/ Open an InputStream to the file's absolute path, and push that to the
client via the Response object's OutputStream. This works best if the
specified files exist outside of the webapp context, since the webapp
may be sealed inside a WAR and thus the notion of "absolute path" goes
out the window...
-QM
--
software -- http://www.brandxdev.net
tech news -- http://www.RoarNetworX.com
---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org