tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Shapira, Yoav" <>
Subject RE: How to connect to a Data-File above the WEB-INF-Folder from a servlet????
Date Tue, 03 Dec 2002 15:22:19 GMT

>How do I connect to a Data-File above the WEB-INF-Folder from a
>For example my class is placed in
>and my data-files are placed in $CATALINA_HOME/webapps/data/data.dat or
>so. Is this possible or does anybody know a better way to place my
>data-files? And how do I connect to them?

Data files belonging to or referenced by your webapp should be under
your webapp's structure (or within your webapps .war file).  So if your
app is in the ROOT context, your data files should be in

Then, from your servlets, you could do:

InputStream is =
(and do whatever you need with the input stream, e.g. read it)
URL fileUrl = getServletContext().getResource("/data/data.dat");
To just get the URL for the file.  Don't assume this is a file:// or an
http:// URL.  It may be a jndi:/localhost/... type of URL as well.

You could also use 
String filePath = getServletContext().getRealPath("/data/data.dat");
approach, but that won't work if you're deploying out of a .war file.

Finally, if for some reason you cannot place your data files under your
own context, you can set a data file directory as a context parameter in
your web.xml.  For example:

Then in your servlets, you could do:
String dataFileDirectory =

String filePath = dataFileDirectory + File.separator + "data.dat";
And go from there.

I hope you find this helpful,

Yoav Shapira
Millennium ChemInformatics

To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message